1👍
✅
replace <a href="{{ post.slug }}">{{ post.title }}</a>
with <a href="/{{ post.slug }}">{{ post.title }}</a>
The issue is if the reference in anchor doesnt starts with /
it means its a relative url and that reference is added to current url. Hope this helps you. Also for further reference please use absolute_url feature of django. Please read this 🙂
0👍
Use the url template tag:
{% for post in posts %}
<h3><a href="{% url 'post' slug=post.slug %}">{{ post.title }}</a></h3>
{% endfor %}
This reconstruct the url based on your url configuration, ensuring it is a valid url. Here, 'post'
is the name of the url you defined in your urlconf, and slug=post.slug
is the parameter you pass to the capturing group in your url regex.
👤knbk
- Django login users to their portal
- How do I prevent form submission if the form is invalid?
- How to use variable from looping as dictionary key in Django template?
- How to pre-select dropdown on Django?
- Working example of tweet with image using javascript django
Source:stackexchange.com