我是Django Environment的新手(当前版本:1.11.4),我试图找到一个HTML页面来查看问题,但获得例外:
NoReverseMatch at /music/
Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: [u'music/(?P<pk>[0-9]+)/$']
我正在考虑的一些文件是:
的index.html
{% extends 'music/base.html' %}
{% block body %}
{% if all_albums %}
<ul>
{% for album in all_albums %}
<li>
<a href="{% url 'music:detail' album.id %}">{{album.album_title}}</a> </li>
{% endfor %}
</ul>
{% else %}
<h3>No Album Found </h3>
{% endif %}
{% endblock %}
urls.py
from django.conf.urls import url
from . import views
app_name = 'music'
urlpatterns = [
url(r'^$',views.IndexView.as_view(), name = 'index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name = 'detail'),
]
views.py
from django.views import generic
from .models import Album
from django.views.generic.edit import CreateView , UpdateView , DeleteView
from django.core.urlresolvers import reverse_lazy
class IndexView(generic.ListView):
template_name = 'music/index.html'
context_object_name = 'all_albums'
def get_queryset(self):
return Album.objects.all()
class DetailView(generic.DetailView):
model = Album
template_name = 'music/detail.html'
和我的 base.html 文件只有一个导航栏,其中包含一些功能。我只是尽一切努力让它发挥作用,但直到我无法做到这一点,你能帮助我吗?
修改
models.py
from __future__ import unicode_literals
from django.db import models
from django.core.urlresolvers import reverse
# Create your models here.
class Album(models.Model):
artist = models.CharField(max_length = 250)
genre = models.CharField(max_length = 500)
album_title = models.CharField(max_length = 250)
album_logo = models.CharField(max_length = 1000)
def get_absolute_url(self):
return reverse('music:detail', kwargs = {'pk': self.pk})
def __str__(self):
return self.album_title + ' : ' + self.artist
class Song(models.Model):
album = models.ForeignKey(Album , on_delete = models.CASCADE)
file_type = models.CharField(max_length = 10)
song_title = models.CharField(max_length = 250)
is_favourite = models.BooleanField(default = False)
def __str__(self):
return self.song_title