适用于参数的Python NoReverseMatch

时间:2018-07-06 03:26:28

标签: python django

我是Python Django的新手,我正在努力应对URL模式,我正在创建的网站包含三个HTML页面和它们各自的三个视图,我正在查看我的代码和项目结构的图像,请帮助我找到解决方案: 项目结构: enter image description here

所以想法是,当我单击index.html时,页面将重定向到具有图像ID(例如“ music / id”)的detail.html,现在我想重定向至图像上的其他html页面,请在detail.html中单击到picturedetail.html。具有“ song_title”属性

代码:-

  1. Models.py:-

    from django.db import models
    
    
    
    class Album(models.Model):
      art_type=models.CharField(max_length=255)
      album_title=models.CharField(max_length=255)
      art_method=models.CharField(max_length=255)
      album_logo=models.FileField()
    
    def __str__(self):
        return self.album_title;
    
    
    class Picture(models.Model):
      album=models.ForeignKey(Album,on_delete=models.CASCADE)
      file_type=models.FileField()
      song_title=models.CharField(max_length=245)
    
  2. detail.html:-

      {% extends 'music/base.html'  %}
      {% block title %}Album Details{% endblock %}
      {% block body %}
      <ul>
         {% for picture in album.picture_set.all %}
          <div class="col-sm-4 col-lg-2">
               <div class="thumbnail">
                  <a href="{% url 'music:picturedetail' picture.song_title %}">
                   <img src="{{ picture.file_type.url }}" class="img-responsive">
                            </a>
                            <div class="caption">
                              <h6>{{picture.song_title}}</h6>
                            </div>
                    </div>
    
            </div>
    
         {% endfor %}
      </ul>
    

    {%endblock%}

  3. urls.py:-

    从django.conf.urls

    导入URL     来自。导入视图

    app_name='music';
    urlpatterns = [
    
        url(r'^$',views.IndexView.as_view(),name='index'),
    
        # /music/id/
        url(r'^(?P<pk>[0-9]+)$',views.DetailView.as_view(),name='detail'),
        #for PictureDetail view
        url(r'^(?P<pk>[0-9]+)/(?P<song_title>[A-Za-z]+)', views.PicturedetailView.as_view(), name='picturedetail'),
    
    ]
    

4.view.py:-

from django.views import generic
from .models import Album
from .models import Picture



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 '


class PicturedetailView(generic.DetailView):
    model =Picture
    template_name = 'music/picturedetail.html'

我遇到的错误是:-

  NoReverseMatch at /music/1
Reverse for 'picturedetail' with arguments '('River',)' not found. 1 pattern(s) tried: ['music/(?P<pk>[0-9]+)/(?P<song_title>[A-Za-z]+)']
Request Method: GET
Request URL:    http://127.0.0.1:8000/music/1
Django Version: 2.0.6
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'picturedetail' with arguments '('River',)' not found. 1 pattern(s) tried: ['music/(?P<pk>[0-9]+)/(?P<song_title>[A-Za-z]+)']
Exception Location: C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.6-py3.6.egg\django\urls\resolvers.py in _reverse_with_prefix, line 636
Python Executable:  C:\Users\dell\AppData\Local\Programs\Python\Python36-32\python.exe
Python Version: 3.6.1
Python Path:    
['D:\\Django\\website',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\django-2.0.6-py3.6.egg',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\pytz-2018.4-py3.6.egg']
Server time:    Fri, 6 Jul 2018 03:00:51 +0000

1 个答案:

答案 0 :(得分:2)

url(r'^(?P<pk>[0-9]+)/(?P<song_title>[A-Za-z]+)', views.PicturedetailView.as_view(), name='picturedetail'),

这有两个参数pk和song_title。请尝试以下操作:

{% url 'music:picturedetail' pk=picture.pk song_title=picture.song_title %}