Manoj Kamble
Manoj Kamble

Reputation: 595

In django how In can pass data through URL?

I want to pass the model's id through the URL. I have created a path and view according but still I get NoReverseMatch at /url error.

Here's the code that I used in my project:

In template_name.html :

<a href="{% url 'url_name' model.id %}">

In view.py :

def view_name(request, id):

In urls.py:

path('url_name/<int:id>', view_name, name='url_name')

Upvotes: 2

Views: 536

Answers (2)

Chymdy
Chymdy

Reputation: 660

it is advisable to always add a trailing slash to your urls like so:

path('url_name/<int:id>/', view_name, name='url_name')

Hope it solves your problem.

Upvotes: 3

Hamid Rasti
Hamid Rasti

Reputation: 975

You can pass path param like this:

<a href={% url 'url_name' id=model.id %}>Click</a>

Upvotes: 0

Related Questions