Django - 得到错误“Reverse for 'detail',没有找到任何参数.尝试了 1 个模式:"当使用 {% url "music:fav";%} [英] Django - getting Error "Reverse for 'detail' with no arguments not found. 1 pattern(s) tried:" when using {% url "music:fav" %}
问题描述
我从过去 4 天开始学习 django 框架.今天我试图通过使用
I am learning django framework from last 4 days. Today I was trying to retrieve a URL in HTML template by using
{% url "music:fav" %}
{% url "music:fav" %}
我将 music/urls.py 中的命名空间设置为
where I set the namespace in music/urls.py as
app_name="音乐"
app_name= "music"
而且我还有一个名为 fav() 的函数.代码如下:
and also I have a function named fav(). Here is the codes:
音乐/urls.py
from django.urls import path
from . import views
app_name = 'music'
urlpatterns = [
path("", views.index, name="index"),
path("<album_id>/", views.detail, name="detail"),
path("<album_id>/fav/", views.fav, name="fav"),
]
音乐/views.py
music/views.py
def fav(request):
song = Song.objects.get(id=1)
song.is_favorite = True
return render(request, "detail.html")
我使用的detail.html
in detail.html I used
{% url 'music:fav' %}
但我不知道为什么会显示此错误:
But I dont know why this is showing this error:
NoReverseMatch at/music/1/Reverse for 'detail' with no arguments not成立.尝试了 1 个模式:['music/(?P[^/]+)/$']
NoReverseMatch at /music/1/ Reverse for 'detail' with no arguments not found. 1 pattern(s) tried: ['music/(?P[^/]+)/$']
推荐答案
path("<album_id>/fav/", views.fav, name="fav"),
此 URL 需要 album_id.像这样:
This URL needs the album_id. Something like this:
{% url 'music:fav' 1 %}
{% url 'music:fav' album.id %}
这篇关于Django - 得到错误“Reverse for 'detail',没有找到任何参数.尝试了 1 个模式:"当使用 {% url "music:fav";%}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!