Django: Is there anyway to specify a django url path for a template of the item currently being iterated in a forloop
I have a simple unordered list of entries that have been iterated over a Django forloop like this.
<ul>
{% for entry in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
However, I wanted to make all of the listed items in the forloop to be links to a template html file. Which brings the question, is there a way to format my code to where the URL pathway can take the name of the entry to link it to the desired template for it? Something like this.
<ul>
{% for entry in entries %}
<li><a href="{% url 'encyclopedia/{{ entry }}' %}">{{ entry }}</a></li>
{% endfor %}
</ul>
obviously this code doesn't work, but is there something similar that I can do?
Heres my urls.py and views.py for more information.
urls.py
path("<str:TITLE>", views.TITLE, name="TITLE")
views.py
def TITLE(request, TITLE):
# util.markdownify(f"{TITLE}.html", f"{TITLE}.md")
return render(request, f"encyclopedia/{TITLE}.html")
I try to give you an answer, I don't know your models so you have to adapt the code probably. urls.py
path("<str:title>", views.title, name="title") #I suggest to use lowercase letters
template
<ul>
{% for entry in entries %}
<li><a href="{% url 'encyclopedia' title=entry.title %}">{{ entry.title }}</a></li>
{% endfor %}
</ul>
Now you can create an encyclopedia template with your title view
views.py
def title(request, title):
entry = YourModel.objects.get(title=title)
return render(request, encyclopedia.html", {'entry ': entry })
And your encyclopedia.html file could be something like this: encyclopedia.html
{{entry.title}} //or whatever you need in your template