I am encountering a strange django behavior that I don’t see where it could go wrong.
I have the start of an app that returns dynamic pages whose url is created through the slug.
Everything works well. With any slug I try it works perfectly and the urls find their path without problem. They all go through the same path.
But there is ONLY one slug that does not find the path.
The peculiarity of the behavior is that in the browser’s url it is the only <a href>
that adds the /
to the url, even though in the html code it does not have the /
in the href.
I don’t understand when the /
is added, since all the pages are generated dynamically and go through the same process.
This is the page sended to the browser with all links. The url is made dinamically with the slug of the model of each post.
<h2><a href="/posts/first-post"> my post first </a></h2>
<h2><a href="/posts/fourth-post"> my 4th post </a></h2>
<h2><a href="/posts/ready-for-summer"> Ready for Summer Fun! </a></h2>
<h2><a href="/posts/third-post"> my 3th post </a></h2>
When you click to each links, everything goes fine and the page with the post is sended.
the url in the browser after clicking is:
http://localhost:8000/posts/first-post/
<<<<<——— /
showing up
http://localhost:8000/posts/fourth-post
http://localhost:8000/posts/ready-for-summer
http://localhost:8000/posts/third-post
AS you can see the only time when the /
at the end of the url appers is with first-post/
.
I don’t understand why this is happening.
I have try different slugs for the same ‘post’ and with all works find, the /
is not add at the end, and the page is sended with no problems. ONLY when I use the slug ‘first-post’ that the /
is added.
html where the <a href>
is generated:
{% block content %}
<section>
<h1>Post List</h1>
{% for post in posts %}
<article class='post'>
<h2><a href="{% url 'posts:page' slug=post.slug %}"> {{ post.title }} </a></h2>
<p>{{ post.date }}</p>
<p>{{ post.body }}</p>
</article>
{% endfor %}
</section>
{% endblock %}
urls.py of the app
app_name= 'posts' #defina namespace
urlpatterns = [
path('', views.posts_list, name='posts_list'),
path('<slug:slug>', views.post_page, name='page'),
]
views.py of the app:
def post_page(request, slug):
try:
post = Post.objects.get(slug=slug)
print('TEST Post found:', post)
except Post.DoesNotExist:
print('TEST Post not found')
return HttpResponse('Post not found', status=404)
return render(request, 'posts/post_page.html', {'post': post})
query from the shell, where you can see the slug ‘first-post‘:
>>> from post.models import Post
>>> post = Post.objects.get(slug='first-post')
>>> print(f"'{post.slug}'")
'first-post
The strange thing is that all ‘post’ goes through the same process, and all slugs work fine, ONLY ‘first-post’ fails.
I don’t know if the problem is in the browser when adds the /
only to the
http://localhost:8000/posts/first-post/
.
I have check everything.
I can fix the issue by adding the /
in the path <slug:slug>/
,
path('<slug:slug>/', views.post_page, name='page')
But that doesn’t solve the the problem, just change the way of refering to the path. But that change makes the browser url have /
behind an html resource, something I don’t want.
The question then is WHY the /
is added so arbitrarily only whith ‘first-post’.
I have try everything in my knowledge.