[Fixed]-Connecting URL in django

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

Leave a comment