404 error

14 views
Skip to first unread message

Spyke Lionel

unread,
Sep 12, 2020, 3:17:07 AM9/12/20
to Django users
I have this urlpatterns..

urlpatterns = [
    path('', views.index, name='index'),
    path('<int:album_id>/', views.detail, name='detail'),
]

my index.html looks like this

<h1>Albums to display</h1>
<ul>
    {% for album in all_albums %}
    <li><a href="/music/{{ album_id }}/"> {{ album.album_title }}</a></li>
    {% endfor %}
</ul>
#my index.html simply displays my albums. and the above works just fine

my detail.html looks like this

{{ album }}
#and it works just fine too

my views.py looks like this

def index(request):
    all_albums = Album.objects.all()
    return render(request, 'music/index.html', {'all_albums': all_albums})

def detail(request, album_id):
    try:
        album = Album.objects.get(pk=album_id)
    except Album.DoesNotExist:
        raise Http404("Album does not exist")
    return render(request, 'music/detail.html', {'album': album})

#when ever I click on an album link to get the album details, I get the 404 below:

Page not found (404)
Request Method: GET

Using the URLconf defined in website.urls, Django tried these URL patterns, in this order:
1. admin/
2. music/ [name='index']
3. music/ <int:album_id>/ [name='detail']
The current path, music//, didn't match any of these.

in my detail funtion in views.py, at first I used on request as an argument and it worked just fine to give me the album details (which returned only the album id number), but when i added album_id, so as to get the album details, I got an error. saying music// not found. Now I don't understand how the last forward slash(/) was added.
can I get the explaination to this. Thanks 

Amitesh Sahay

unread,
Sep 12, 2020, 3:23:08 AM9/12/20
to Django users
At the first glance, I think you havn't created URL pattern for "music". May be....just saying.

Regards,
Amitesh 


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/36d25ae2-7e58-43dc-ad0f-83da9e55e526n%40googlegroups.com.

Kunal Solanke

unread,
Sep 12, 2020, 3:26:39 AM9/12/20
to django...@googlegroups.com
I think he have created the music url,
But the {{album_id}} is not properly passed as context from view.


Kunal Solanke

unread,
Sep 12, 2020, 3:29:17 AM9/12/20
to django...@googlegroups.com
In your template it should be {{album.id}}
or {{album.album_id}} depending how your models are.I am talling about href in anchor tag.

Amitesh Sahay

unread,
Sep 12, 2020, 3:29:38 AM9/12/20
to django...@googlegroups.com

try something like


Regards,
Amitesh 


Spyke Lionel

unread,
Sep 12, 2020, 4:36:10 AM9/12/20
to django...@googlegroups.com
I don't understand how the url became 
http://127.0.0.1:8000/music//
Instead of http://127.0.0.1:8000/music/<int:alb_id>/


Kunal Solanke

unread,
Sep 12, 2020, 4:55:29 AM9/12/20
to django...@googlegroups.com

Spyke Lionel

unread,
Sep 12, 2020, 6:28:13 AM9/12/20
to django...@googlegroups.com
yes I did. it should work properly. 
I just wanna display a list of items after the link has been clicked. 
But it happens that I get http://127.0.0.1:8000/music//
The last forward slash shouldn't have added.
This work when the integer is inserted manually on the address. 
But I want it to happen this way when a link is clicked 

Reply all
Reply to author
Forward
0 new messages