Multiple Parameters Url Pattern Django 2.0
I want to pass two parameters in my url pattern but i am getting error no-reverse match i.e 'projects'.While it works fine with only one parameter. here is main urls file- urlpatte
Solution 1:
No regex matching or additional model fields required to make it work.
urls.py
path('custom_page/<str:id1>/<str:id2>/', views.custom_page, name='custom_page'),
views.py
def custom_page(request, id1, id2):
#use in view func or pass to template via context
context = {}
context['id1'] = id1
context['id2'] = id2
return render(request, 'custom_page.html', context=context)
custom_page.html
<div>{{id1}} {{id2}}</div>
Solution 2:
You can do something as below.
Old Way
(r'^view_url/(\d+)/(\d+)$', r'app_name.views.view_function'),
def view_function(request, param1, param2):
"""
:param request:
:param param1:
:param param2:
:return:
"""
return render('/* template path and parameters */')
New Way
(r'^view_url/<int:param1>/<int:param2>$', r'app_name.views.view_function'),
def view_function(request, param1, param2):
"""
:param request:
:param param1:
:param param2:
:return:
"""
return render('/* template path and parameters */')
For more details regex pattern in django 2.0 you can check django documentation link. https://docs.djangoproject.com/en/2.1/topics/http/urls/
Solution 3:
You can do what you want much more easily (without regular expressions) like so. You can find the documentation details here.
path('some_page/<int:project_id>/', views.some_page, name='some_page'),
And the function for my urlpattern above would look like this:
def some_page(request, project_id):
project = Project.objects.get(id=project_id)
return render(request, 'project.html', {'project': project})
Just remember that you need a Project
model for this to work properly, with data in it.
And your template would be like:
<p>{{ project.modelfieldname1 }}</p>
<p>{{ project.modelfieldname2 }}</p>
Post a Comment for "Multiple Parameters Url Pattern Django 2.0"