10

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 %}"
Lavanya Pant
  • 702
  • 7
  • 23

4 Answers4

15

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>
philmaweb
  • 514
  • 7
  • 13
3

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/

Lavanya Pant
  • 702
  • 7
  • 23
1

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>
0

I have a similar implementation, hope it will be of help to someone.

Django template (html file)

<a href="{% url 'app:single-rfq' key='view' id=rfq.id %}">

Application URL file

path('single-rfq/<str:key>/<int:id>/', views.single_rfq, name='single-rfq'),

Views File

@login_required(login_url='/')    
def single_rfq(request, key, id):
Kevin Owino
  • 472
  • 3
  • 7