Home > Blockchain >  Get html link id in django views.py
Get html link id in django views.py

Time:03-23

This is what my template index.html looks like

{% extends "encyclopedia/layout.html" %}

{% block title %}
    Encyclopedia 
{% endblock %}

{% block body %}

<h1>All Pages</h1>

<ul>
    {% for entry in entries %}
    <li><a href="{% url 'link' %}">{{ entry }}</a></li>
    {% endfor %}
</ul>

{% endblock %}

And here is my urls.py file

path("link", views.link, name="link")

Finally, this is my link view inside of the views.py file

def link(request, id):
    return redirect("page", name=id)

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

CodePudding user response:

Replace your statement with this line inside index.html.

<li><a href="{% url 'link' entry.id %}">{{ entry }}</a></li>

Your urls.py should contain

path("link/<int:id>", views.link, name="link")

Now, you should get the id in the id variable of the views.py when the tag is clicked.

  • Related