1

I am trying to get url parameters like this:

<tbody>
{% for object in objects %}
<tr>
    <td>{{ object.id }}</td>
    <td>{{ object.user }}</td>
    <td>{{ object.url }}</td>
    <td>{{ object.minimum }}</td>
    <td>{{ object.maximum }}</td>
    <td>{{ object.requests }}</td>
    <td>{{ object.stay }}</td>
    <td class="text-Centre">
        <a href="{% url 'trafficapp:generate_traffic/?id={{object.id}}&?user={{object.user}}' %}" class="btn btn-danger btn-round remove">Run</a>
    </td>
</tr>
{% endfor %}
</tbody>

If I put my URL like this, it works since the view exists in view.py: {% url 'trafficapp:generate_traffic' %}

Now, I am trying to take URL parameters with it, but I am unable to make how can I pass data from this button, and what should I put in my URL pattern for this. Kindly help, I am new to Django and still figuring out how it works.

Maisum Abbas
  • 317
  • 1
  • 10

1 Answers1

2

you can add parameters after {% url %}

<a href="{% url 'trafficapp:generate_traffic' %}?id={{object.id}}&user{{object.name}}" class="btn btn-danger btn-round remove">Run</a>
Andrey Maslov
  • 1,396
  • 1
  • 8
  • 10
  • 1
    But, how should the url be written in urls.py file? Should it be like this: `path('generate_traffic', views.generate_traffic, name='generate_traffic')`? – Maisum Abbas Sep 18 '20 at 09:56
  • 1
    also yo don't need `?` for the second paameter it should be like `?id={{object.id}}&user={{object.name}}` – Pulath Yaseen Sep 18 '20 at 09:57
  • i was wrong in general you don't need `/` you url config will add it if needed.(modified answer) – Andrey Maslov Sep 18 '20 at 09:57
  • you don't have to specify the arguments that are passed through GET method. They doesn't need to be specified in urls. – Pulath Yaseen Sep 18 '20 at 09:58
  • @PulathYaseen so if I just access this 'path('generate_traffic', views.generate_traffic, name='generate_traffic')' without specifying arguments, I will be able to get my data in 'generate_traffic', you mean? – Maisum Abbas Sep 18 '20 at 10:01