173

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?

Mr Singh
  • 3,936
  • 5
  • 41
  • 60
Atma
  • 29,141
  • 56
  • 198
  • 299
  • 1
    Check this [link](https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#url). – Nagkumar Arkalgud Nov 30 '15 at 09:23
  • Possible duplicate of [Is it possible to pass query parameters via Django's {% url %} template tag?](http://stackoverflow.com/questions/4591525/is-it-possible-to-pass-query-parameters-via-djangos-url-template-tag) – Ciro Santilli OurBigBook.com May 12 '16 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//')`. 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. – Christian Long May 08 '19 at 20:15
  • See [this answer](https://stackoverflow.com/a/51377425/456550) for a simple custom template tag to render url query strings. – Christian Long May 08 '19 at 20:25

6 Answers6

246

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 %}
andergmartins
  • 2,738
  • 2
  • 13
  • 7
  • @MehranNouri use request.GET instead of request.get – Cyd Jul 14 '19 at 10:16
  • 1
    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 – teebagz Nov 06 '19 at 16:30
  • 16
    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). – BjornW May 18 '20 at 12:21
155

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]

Community
  • 1
  • 1
Atma
  • 29,141
  • 56
  • 198
  • 299
  • 1
    If use this, the information 'office=foobar' will be visible to user on the navigation bar, isn't it? – Diansheng Feb 27 '17 at 14:53
  • 1
    Doesn't this result in the trailing slash interrupting the url, like `example.com/myview/?office=foobar` instead of `example.com/myview?office=foobar`? – Al Sweigart May 12 '17 at 02:27
  • 3
    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 – Shayne Dec 30 '17 at 06:59
  • 1
    what passing two arguments? – FightWithCode Jul 31 '19 at 16:17
41

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')
daaawx
  • 3,273
  • 2
  • 17
  • 16
39

Simply add Templates URL:

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

Used in django 2.0

Mr Singh
  • 3,936
  • 5
  • 41
  • 60
  • @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. – Bernardo Duarte Jun 11 '20 at 16:45
15

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

Guillaume Lebreton
  • 2,586
  • 16
  • 25
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.

Cyd
  • 1,245
  • 1
  • 14
  • 17