反转&#39;详细信息&#39;参数&#39;()&#39;和关键字参数&#39; {&#39; pk&#39;:2}&#39;未找到。尝试了1种模式:[&#39;音乐/(?P <album_id> [0-9] +)/ $&#39;]

时间:2016-08-28 13:44:02

标签: python django

我的追溯:

File "D:\virtualEnv\lib\site-packages\django\core\handlers\exception.py" in inner
  39.             response = get_response(request)

File "D:\virtualEnv\lib\site-packages\django\core\handlers\base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "D:\virtualEnv\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "D:\virtualEnv\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "D:\virtualEnv\lib\site-packages\django\views\generic\base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "D:\virtualEnv\lib\site-packages\django\views\generic\base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "D:\virtualEnv\lib\site-packages\django\views\generic\edit.py" in post
  217.         return super(BaseCreateView, self).post(request, *args, **kwargs)

File "D:\virtualEnv\lib\site-packages\django\views\generic\edit.py" in post
  183.             return self.form_valid(form)

File "D:\virtualEnv\lib\site-packages\django\views\generic\edit.py" in form_valid
  163.         return super(ModelFormMixin, self).form_valid(form)

File "D:\virtualEnv\lib\site-packages\django\views\generic\edit.py" in form_valid
  79.         return HttpResponseRedirect(self.get_success_url())

File "D:\virtualEnv\lib\site-packages\django\views\generic\edit.py" in get_success_url
  151.                 url = self.object.get_absolute_url()

File "D:\virtualEnv\website\music\models.py" in get_absolute_url
  13.         return reverse('music:detail', kwargs={'pk': self.pk})

File "D:\virtualEnv\lib\site-packages\django\urls\base.py" in reverse
  91.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "D:\virtualEnv\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
  389.             (lookup_view_s, args, kwargs, len(patterns), patterns)

Exception Type: NoReverseMatch at /music/album/add/
Exception Value: Reverse for 'detail' with arguments '()' and keyword arguments '{'pk': 2}' not found. 1 pattern(s) tried: ['music/(?P<album_id>[0-9]+)/$']

我的相册模型是:

class Album(models.Model):
    artist = models.CharField(max_length=250)
    album_title = models.CharField(max_length=500)
    genre = models.CharField(max_length=100)
    album_logo = models.CharField(max_length=1000)

    def get_absolute_url(self):
        return reverse('music:detail', kwargs={'pk': self.pk})

my views.py:

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 AlbumCreate(CreateView):
    model = Album
    fields = ['artist', 'album_title','genre', 'album_logo']

网址:

app_name = 'music'    
urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<album_id>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
    url(r'album/add/$', views.AlbumCreate.as_view(), name='album-add'),
    ]

为什么此代码在get_absolute_url中的D:\ virtualEnv \ website \ music \ models.py中无法正常运行:

        return reverse('music:detail', kwargs={'pk': self.pk}) 

当我尝试使用表单添加相册时,它会出现上述错误。我该如何解决这个问题?

修改

当我用pk更改album_id时,会出现以下错误:

Template error:
In template D:\virtualEnv\website\music\templates\music\detail.html, error at line 14
   Reverse for 'favorite' with arguments '(4,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []   4 : 
   5 : <img src="{{album.album_logo}}" >
   6 : 
   7 : <h1>{{album.album_title }}</h1>
   8 : <h3>{{album.artist}}</h3>
   9 : 
   10 : {% if error_message %}
   11 :     <p><strong>{{ error_message }}</strong></p>
   12 : {% endif %}
   13 : 
   14 : <form action=" {% url 'music:favorite' album.id %} " method="post">
   15 :     {% csrf_token %}
   16 :     {% for song in album.song_set.all %}
   17 :     <input type="radio" id="song{{ forloop.counter}}" name="song" value="{{song.id}}">
   18 :     <label for= "song{{ forloop.counter}}">
   19 :         {{song.song_title}}
   20 :         {% if song.is_favorite %}
   21 :             <img src="http://i.imgur.com/b9b13Rd.png"/>
   22 :         {% endif %}
   23 :     </label><br>
   24 :     {% endfor %}


Traceback:

File "D:\virtualEnv\lib\site-packages\django\core\handlers\exception.py" in inner
  39.             response = get_response(request)

File "D:\virtualEnv\lib\site-packages\django\core\handlers\base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "D:\virtualEnv\lib\site-packages\django\core\handlers\base.py" in _get_response
  217.                 response = self.process_exception_by_middleware(e, request)

File "D:\virtualEnv\lib\site-packages\django\core\handlers\base.py" in _get_response
  215.                 response = response.render()

File "D:\virtualEnv\lib\site-packages\django\template\response.py" in render
  109.             self.content = self.rendered_content

File "D:\virtualEnv\lib\site-packages\django\template\response.py" in rendered_content
  86.         content = template.render(context, self._request)

File "D:\virtualEnv\lib\site-packages\django\template\backends\django.py" in render
  66.             return self.template.render(context)

File "D:\virtualEnv\lib\site-packages\django\template\base.py" in render
  208.                     return self._render(context)

File "D:\virtualEnv\lib\site-packages\django\template\base.py" in _render
  199.         return self.nodelist.render(context)

File "D:\virtualEnv\lib\site-packages\django\template\base.py" in render
  994.                 bit = node.render_annotated(context)

File "D:\virtualEnv\lib\site-packages\django\template\base.py" in render_annotated
  961.             return self.render(context)

File "D:\virtualEnv\lib\site-packages\django\template\loader_tags.py" in render
  174.         return compiled_parent._render(context)

File "D:\virtualEnv\lib\site-packages\django\template\base.py" in _render
  199.         return self.nodelist.render(context)

File "D:\virtualEnv\lib\site-packages\django\template\base.py" in render
  994.                 bit = node.render_annotated(context)

File "D:\virtualEnv\lib\site-packages\django\template\base.py" in render_annotated
  961.             return self.render(context)

File "D:\virtualEnv\lib\site-packages\django\template\loader_tags.py" in render
  70.                 result = block.nodelist.render(context)

File "D:\virtualEnv\lib\site-packages\django\template\base.py" in render
  994.                 bit = node.render_annotated(context)

File "D:\virtualEnv\lib\site-packages\django\template\base.py" in render_annotated
  961.             return self.render(context)

File "D:\virtualEnv\lib\site-packages\django\template\defaulttags.py" in render
  447.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "D:\virtualEnv\lib\site-packages\django\urls\base.py" in reverse
  91.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "D:\virtualEnv\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
  389.             (lookup_view_s, args, kwargs, len(patterns), patterns)

Exception Type: NoReverseMatch at /music/4/
Exception Value: Reverse for 'favorite' with arguments '(4,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

1 个答案:

答案 0 :(得分:0)

您的get_absolute_url方法传递了一个名为kwarg的{​​{1}},但您的网址格式需要一个名为pk的参数。