This is what my template index.html
looks like
JavaScript
x
18
18
1
{% extends "encyclopedia/layout.html" %}
2
3
{% block title %}
4
Encyclopedia
5
{% endblock %}
6
7
{% block body %}
8
9
<h1>All Pages</h1>
10
11
<ul>
12
{% for entry in entries %}
13
<li><a href="{% url 'link' %}">{{ entry }}</a></li>
14
{% endfor %}
15
</ul>
16
17
{% endblock %}
18
And here is my urls.py
file
JavaScript
1
2
1
path("link", views.link, name="link")
2
Finally, this is my link
view inside of the views.py
file
JavaScript
1
3
1
def link(request, id):
2
return redirect("page", name=id)
3
Where page()
is a function that takes one argument. I want the id
of the <a></a>
tag of index.html
to be that argument. But I have no idea how to access get that id inside of views.py
when the <a></a>
tag is clicked
Advertisement
Answer
Replace your statement with this line inside index.html
.
JavaScript
1
2
1
<li><a href="{% url 'link' entry.id %}">{{ entry }}</a></li>
2
Your urls.py
should contain
JavaScript
1
2
1
path("link/<int:id>", views.link, name="link")
2
Now, you should get the id in the id
variable of the views.py
when the tag is clicked.