Reputation: 73
In Chrome, I must manually type http://127.0.0.1:8000/music/1/
in order to get to the page that I want. (I want to go to page 1).
However, When I try to click the link that I thought would take me to where I want in http://127.0.0.1:8000/music
, for example, Red. It takes me to that error page:
Here's my views.py
:
from django.http import HttpResponse
from django.shortcuts import loader
from .models import Album
def index(request):
all_albums = Album.objects.all()
template = loader.get_template('music/index.html')
context = {
'all_albums': all_albums,
}
return HttpResponse(template.render(context, request))
def detail(request, album_id):
return HttpResponse("<h2>Details for album id: " + str(album_id) + "</h2>")
Here's my urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name = 'index'),
url(r'^(?P<album_id>[0-9]+)/$', views.detail, name = 'detail'),
]
Here's my `index.html
{% if all_albums %} <!-- Will be true as long as it has at least 1 album -->
<h3>Here are all my albums</h3>
<ul>
{% for album in all_albums %}
<li><a href="/music/id/{{ album.id }}">{{ album.album_title }}</a></li>
{% endfor %}
</ul>
{% else %}
<h3>You don't have any albums</h3>
{% endif %}
Upvotes: 2
Views: 104
Reputation: 574
There's an error in the template. Instead of /music/id/{{ album.id }}
you should have /music/{{ album.id }}/
. When you hit the link Red
it redirects you to music/id/1
instead of music/1
. So you get a 404 error.
Upvotes: 2