multiple parameters url pattern django 2.0

user9723456 picture user9723456 · Jul 22, 2018 · Viewed 16.3k times · Source

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-

urlpatterns = [
    path('admin/', admin.site.urls),
    path(r'^materials/(?P<name>(\s+)/',include('materials.urls')),
    path(r'^projects/',include('projects.urls')),
]

projects.urls-

urlpatterns = [
path('',views.view_projects,name='view_projects'),
path('(?<projectid>\d+)/',views.project_steps,name='project_steps'),
path('(P<projectid>\d+)/(P<stepid>\d+)/',views.project_steps,
name='project_steps'),
] 

views.py-

 def view_projects(request):
   projects = project.objects.all
   return render(request,'projects/project_view.html', 
   {'projects':projects})

def project_steps(request,projectid,stepno=1):
  projects = project.objects.all
  stepss = steps.objects.all
  return render(request,'projects/project_steps.html', 
  {'projectid':projectid,'steps':stepss,'projects':projects,
  'stepno':stepno})

template-

 "{% url 'projects' projectid=project.id stepno=step.step_no %}"

Answer

philmaweb picture philmaweb · May 27, 2019

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>