-1

I have write down this code in a url

path('list/<str:name>', GetList.as_view(), name = "getList" )

my view.py

class BlogList(View):
  def get(self,request, *args, **kwargs):

now i want to set name as optional parameter with list it show show all and with name i will implement query

Jaskaran Singh
  • 145
  • 1
  • 10

1 Answers1

2

There are multiple approaches to do this, One simple approach is to have multiple rules that matches your needs, all pointing to the same view.

urlpatterns = patterns('',
    url(r'^project_config/$', views.foo),
    url(r'^project_config/(?P<product>\w+)/$', views.foo),
    url(r'^project_config/(?P<product>\w+)/(?P<project_id>\w+)/$', views.foo),
)

Also keep in mind that in your view you'll also need to set a default for the optional URL parameter, or you'll get an error:

def foo(request, optional_parameter=''):
    # Your code goes here

For Django version > 2.0

Similar approach , but syntax

urlpatterns = [
    path('project_config/',views.foo,name='project_config'),
    path('project_config/<product>/',views.foo,name='project_config'),
    path('project_config/<product>/<project_id>/',views.foo,name='project_config'),
]
ilyasbbu
  • 1,468
  • 4
  • 11
  • 22