[Django]-Django: pass some paremeter to view in Django urls


If you wish to accept parameter from the client, update your path as below:

path('someurl/<str:somevar>/', someview , name='someurl')

And view now can accept extra parameter:

def someview(request, somevar):
    # now you can use somevar

With this definition, if client requests somevar/urlparam/, "urlparam" will be passed to you view function.

Otherwise if you want to provide your own argument, Django doesn’t provide the way to do it directly in url definition. But, since that variable is your own one, why don’t assign (or compute) that in view? I mean:

def someview(request):
    somevar = "test"  # or you may call some function for dynamic assignment
    # now somevar exists in this scope, so you can use it as you want


yes it is possible. You need to define those parameters in the url as pseudo path:

path('articles/<int:year>/<int:month>/', views.month_archive),

There is also an option to use request.GET and request.POST to access the optional parameters list in the url:

 request.POST.get('<par name here>','<default value here>')
 request.GET.get('<par name here>','<default value here>')

Another thing you may find useful is this question.


Leave a comment