Python NoReverseMatch at/ProjektAJ/songs/Reverse for';歌曲';带参数';(';全部';,)';没有找到。尝试了1个模式:[';ProjektAJ\\\/songs/$';]

Python NoReverseMatch at/ProjektAJ/songs/Reverse for';歌曲';带参数';(';全部';,)';没有找到。尝试了1个模式:[';ProjektAJ\\\/songs/$';],python,django,Python,Django,我想从本地sqlite返回项目中所有歌曲的列表 HTML文件 <li class="{% block songs_active %}{% endblock %}"> <a href="{% url 'ProjektAJ:songs' %}"> <span class="glyphicon glyphicon-music" aria-hidden="true"></span>&nbsp; Songs </

我想从本地sqlite返回项目中所有歌曲的列表

HTML文件

<li class="{% block songs_active %}{% endblock %}">
    <a href="{% url 'ProjektAJ:songs' %}">
        <span class="glyphicon glyphicon-music" aria-hidden="true"></span>&nbsp; Songs
    </a>
</li>
<li class="{% block songs_active %}{% endblock %}">
   <a href="{% url 'ProjektAJ:songs' 'all' %}">
       <span class="glyphicon glyphicon-music" aria-hidden="true"></span>&nbsp; Songs
   </a>
</li>
视图.py

url(r'^songs/$', views.SongsView.as_view(), name='songs'),
class SongsView(generic.ListView):
    template_name = 'ProjektAJ/songs.html'
    context_object_name = 'all_songs'

    def get_queryset(self):
        return Song.objects.all()
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
型号.py

url(r'^songs/$', views.SongsView.as_view(), name='songs'),
class SongsView(generic.ListView):
    template_name = 'ProjektAJ/songs.html'
    context_object_name = 'all_songs'

    def get_queryset(self):
        return Song.objects.all()
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

你能告诉我在这种情况下我做错了什么吗?

虽然提供了一些代码来更好地理解这个问题,但url
r'^songs/$”
并没有指向模板所在的文件夹
ProjektAJ
。如果你发布了项目/应用程序的总体结构,可能会更容易提供帮助

感谢您抽出时间和您愿意解决我的问题。 我找到了解决办法

HTML文件

<li class="{% block songs_active %}{% endblock %}">
    <a href="{% url 'ProjektAJ:songs' %}">
        <span class="glyphicon glyphicon-music" aria-hidden="true"></span>&nbsp; Songs
    </a>
</li>
<li class="{% block songs_active %}{% endblock %}">
   <a href="{% url 'ProjektAJ:songs' 'all' %}">
       <span class="glyphicon glyphicon-music" aria-hidden="true"></span>&nbsp; Songs
   </a>
</li>
  • 我必须将
    {%url'ProjektAJ:songs''所有'%}
    添加到我的HTML模板中,然后我将url修改为
    url(r'^songs/(?p[a-zA_Z]+)/$,views.SongsView.as_view(),name='songs'),

    现在它工作正常了。
    谢谢您的时间,我希望本主题对其他有类似问题的人有用。

    此错误确切发生在哪一页?你确定你显示了(足够)正确的模板吗?当我想在主页上单击“歌曲”按钮时,会出现此错误。此时ProjektAJ/songs.html只包含一个字符串“TEST”其余的逻辑是commentHTML代码段是您看到错误时单击的链接,我想?此代码不是由您在此处发布的任何代码引起的。您正在Python中执行
    reverse('songs',('all',)
    ,或者在模板中执行
    {%url'songs'''''%}
    。这就是你需要发布的代码。