Django-在“详细信息”中获取错误“反向”,但未找到参数。尝试了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 = music
app_name= "music"
,还有一个名为fav()的函数。
这是代码:
and also I have a function named fav(). Here is the codes:
music / urls.py
music/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"),
]
music / 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:
在/ music / 1 /上的NoReverseMatch反转'detail',不带参数,不找到
。尝试了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-在“详细信息”中获取错误“反向”,但未找到参数。尝试了1个模式:“当使用{%url“ music:fav” %}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!