我想从本地sqlite返回我项目中所有歌曲的列表
HTML文件
<li class="{% block songs_active %}{% endblock %}">
<a href="{% url 'ProjektAJ:songs' %}">
<span class="glyphicon glyphicon-music" aria-hidden="true"></span> Songs
</a>
</li>
网址.py
url(r'^songs/$', views.SongsView.as_view(), name='songs'),
视图.py
class SongsView(generic.ListView):
template_name = 'ProjektAJ/songs.html'
context_object_name = 'all_songs'
def get_queryset(self):
return Song.objects.all()
型号.py
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_favorite = models.BooleanField(default=False)
def get_absolute_url(self):
return reverse('ProjektAJ:index')
def __str__(self):
return self.song_title
我有如下错误
NoReverseMatch at /ProjektAJ/songs/
Reverse for 'songs' with arguments '('all',)' not found. 1 pattern(s) tried: ['ProjektAJ\\/songs/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/ProjektAJ/songs/
Django Version: 2.0.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'songs' with arguments '('all',)' not found. 1 pattern(s) tried: ['ProjektAJ\\/songs/$']
Exception Location: G:\Python\Python36-32\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 636
Python Executable: G:\Python\Python36-32\python.exe
Python Version: 3.6.3
Python Path:
['C:\\Users\\Adrian Skibiński\\ProbaDjango\\django_projekt',
'G:\\Python\\Python36-32\\python36.zip',
'G:\\Python\\Python36-32\\DLLs',
'G:\\Python\\Python36-32\\lib',
'G:\\Python\\Python36-32',
'G:\\Python\\Python36-32\\lib\\site-packages']
Server time: Sat, 8 Sep 2018 16:29:51 +0000
你能告诉我在这种情况下我做错了什么吗?你知道吗
谢谢你的时间和你愿意解决我的问题。 我找到了解决办法。你知道吗
HTML文件
我不得不将
{% url 'ProjektAJ:songs' 'all' %}
添加到我的HTML模板中,然后我将url修改为url(r'^songs/(?P<filter_by>[a-zA_Z]+)/$', views.SongsView.as_view(), name='songs'),
现在它工作正常了。 感谢您的时间,我希望这个话题将有助于其他类似的麻烦。你知道吗
虽然提供的代码很少,但url
r'^songs/$'
并不指向模板所在的文件夹ProjektAJ
。如果你发布了项目/应用程序的总体结构,也许会更容易提供帮助。你知道吗相关问题 更多 >
编程相关推荐