Passing Multiple Arguments from Django Template Href Link to View

Passing multiple parameters to django url template

You need to open and close the angle brackets (<…>) for each parameter, so:

path(
'coordinator/assign_recommended_project/<str:group_id>/<str:client_id>/<str:project_id>/',
assign_recommended_project,
name='coordinator_assign_project_recommendations',
),

Passing multiple arguments from django template href link to view

This is not how you specify multiple parameters in a URL, typically you write these in the URL, like:

url(
r'^print-permission-document/(?P<studentname>\w+)/(?P<studentsurname>\w+)/(?P<studentclass>\w+)/(?P<doctype>[\w-]+)/$',
print_permission_document, name='print-permission-document'
)

Then you generate the corresponding URL with:

<a href="{% url 'print-permission-document' studentname=studentinfo.0 studentsurname=studentinfo.1 studentclass=studentinfo.2 doctype='doctype-studentlatepermission' %}">Print</a>

This will then generate a URL that looks like:

/print-permission-document/somename/someclass/doctype-studentlatepermission

Typically a path does not contain key-value pairs, and if it does, you will need to "decode" these yourself.

You can also generate a querystring (after the question mark), these you can then access in request.GET [Django-doc].

Django href pass multiple parameters to function

You can define a path with two (or more) parameters with:

path('Up_Items/<int:id_itm>/<int:id_itm2>/', views.ADD_item, name='Up_Items'),

we thus define two variables id_itm and id_itm2, and both have <int:…> as path converter. We use a slash between the two to make it clear where one id stops and the other starts. You can also work with a comma for example, but a slash is more common and a comma should require percent-encoding [wiki].

then we can define a function with:

def ADD_item(request, id_itm, id_itm2):
# …
pass

and refer to it with:

<a href="{% url 'Up_Items' item.id object.id %}">

passing multiple parameters via Django's {% url %} template tags

Remove the extra ?. There should only be one and different query parameters would be separated by an &:

<a href="{% url 'someurl' %}?query1=param1&query2=param2">

Also that or None is unneeded. Change your python code to:

query1 = request.GET.get('query1')
query2 = request.GET.get('query2')

The .get method would either return the value of the provided key or None. If you also provide a default like you did it would return the default (instead of None) if the key doesn't exist.

Djnago Template - Url with multiple parameter

Here is how you can add multiple parameters in URL:

from django.urls import re_path
urlpatterns = [
re_path(r"^resetPasswordSms/(?P<userId>[0-9])/(?P<token>[-\w_=]{28})/$", PasswordResetSmsView.as_view(), name="user_password_sms_reset")
]

https://docs.djangoproject.com/en/3.1/topics/http/urls/#using-regular-expressions

and template URL:

{% url 'pweuser:user_password_sms_reset' token=value1 userId=value2   %}

https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#url

If you have any questions about any part, you can refer to the link below that.



Related Topics



Leave a reply



Submit