183

In my view to get url parameters like this:

date=request.GET.get('date','')

In my url I am trying to pass parameters in this way with the url template tag like this:

<td><a href="{% url 'health:medication-record?date=01/01/2001' action='add' pk=entry.id %}" >Add To Log</a></td>

The parameter after the ? is obviously not working, how can I pass this data value in order to retrieve in with a get?

4
  • 1
    Check this link. Commented Nov 30, 2015 at 9:23
  • 1
    Possible duplicate of Is it possible to pass query parameters via Django's {% url %} template tag? Commented May 12, 2016 at 20:02
  • 4
    When thinking about this, make sure to distinguish between "Django url parameters" and "url query parameters". Django url parameters are configured in urls.py using path() e.g. path('client/<int:id>/'). URL query parameters are the part of the url after the ? e.g. https://example.com/a/b/?param1=value1&param2=value2. This question is about url query parameters, but some of the answers refer to Django url parameters. Commented May 8, 2019 at 20:15
  • See this answer for a simple custom template tag to render url query strings. Commented May 8, 2019 at 20:25

6 Answers 6

253

First you need to prepare your url to accept the param in the regex: (urls.py)

url(r'^panel/person/(?P<person_id>[0-9]+)$', 'apps.panel.views.person_form', name='panel_person_form'),

So you use this in your template:

{% url 'panel_person_form' person_id=item.id %}

If you have more than one param, you can change your regex and modify the template using the following:

{% url 'panel_person_form' person_id=item.id group_id=3 %}
Sign up to request clarification or add additional context in comments.

3 Comments

@MehranNouri use request.GET instead of request.get
Is there anyway to pass a dynamic number of kwargs by passing a dictionary from context? e.g. {% url target_link target_kwargs %} it would be like the equivalent of passing **kwargs to a function. I have a use case where target_link is variable and therefore number of kwargs in url is not know
Note that these are not query parameters, this answer is describing django url parameters. This is not what the OP asked about (the answer does describe a working alternative solution though).
161

I found the answer here: Is it possible to pass query parameters via Django's {% url %} template tag?

Simply add them to the end:

<a href="{% url myview %}?office=foobar">
For Django 1.5+

<a href="{% url 'myview' %}?office=foobar">

[there is nothing else to improve but I'm getting a stupid error when I fix the code ticks]

4 Comments

If use this, the information 'office=foobar' will be visible to user on the navigation bar, isn't it?
Doesn't this result in the trailing slash interrupting the url, like example.com/myview/?office=foobar instead of example.com/myview?office=foobar?
Sean: Yes. If you dont want this, use a POST request, or find some back-channel way to pass the params. Passing params on the URL is standard for GET requests
what passing two arguments?
42

This can be done in three simple steps:

1) Add item id with url tag:

{% for item in post %}
<tr>
  <th>{{ item.id }}</th>
  <td>{{ item.title }}</td>
  <td>{{ item.body }}</td>
  <td>
    <a href={% url 'edit' id=item.id %}>Edit</a>
    <a href={% url 'delete' id=item.id %}>Delete</a>
  </td>
</tr>
{% endfor %}

2) Add path to urls.py:

path('edit/<int:id>', views.edit, name='edit')
path('delete/<int:id>', views.delete, name='delete')

3) Use the id on views.py:

def delete(request, id):
    obj = post.objects.get(id=id)
    obj.delete()

    return redirect('dashboard')

3 Comments

type your code instead of adding a screenshot to avoid downvotes.
Exact thing I was looking for in Django documentation.
I think you need to add double quotes for your href.
40

Simply add Templates URL:

<a href="{% url 'service_data' d.id %}">
 ...XYZ
</a>

Used in django 2.0

1 Comment

@ScottSkiles You should then edit the question to a specific version of Django and then add another one to cover a more recent version. Some would say that's a duplicating, but I disagree. Multiple versions in one question might be misleading.
16

Im not sure if im out of the subject, but i found solution for me; You have a class based view, and you want to have a get parameter as a template tag:

class MyView(DetailView):
    model = MyModel

    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
        ctx['tag_name'] = self.request.GET.get('get_parameter_name', None)
        return ctx

Then you make your get request /mysite/urlname?get_parameter_name='stuff.

In your template, when you insert {{ tag_name }}, you will have access to the get parameter value ('stuff'). If you have an url in your template that also needs this parameter, you can do

 {% url 'my_url' %}?get_parameter_name={{ tag_name }}"

You will not have to modify your url configuration

Comments

9

1: HTML

           <tbody>
            {% for ticket in tickets %}
              <tr>
                <td class="ticket_id">{{ticket.id}}</td>
                <td class="ticket_eam">{{ticket.eam}}</td>
                <td class="ticket_subject">{{ticket.subject}}</td>
                <td>{{ticket.zone}}</td>
                <td>{{ticket.plaza}}</td>
                <td>{{ticket.lane}}</td>
                <td>{{ticket.uptime}}</td>
                <td>{{ticket.downtime}}</td>
                <td><a href="{% url 'ticket_details' ticket_id=ticket.id %}"><button data-toggle="modal" data-target="#modaldemo3" class="value-modal"><i class="icon ion-edit"></a></i></button> <button><i class="fa fa-eye-slash"></i></button>
              </tr>
            {% endfor %}
            </tbody>

The {% url 'ticket_details' %} is the function name in your views

2: Views.py

def ticket_details(request, ticket_id):

   print(ticket_id)
   return render(request, ticket.html)

ticket_id is the parameter you will get from the ticket_id=ticket.id

3: URL.py

urlpatterns = [
path('ticket_details/?P<int:ticket_id>/', views.ticket_details, name="ticket_details") ]

/?P - where ticket_id is the name of the group and pattern is some pattern to match.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.