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" %}
where I set the namespace in music/urls.py as
app_name= "music"
and also I have a function named fav(). Here is the codes:
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
def fav(request):
song = Song.objects.get(id=1)
song.is_favorite = True
return render(request, "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 found. 1 pattern(s) tried: ['music\/(?P[^/]+)\/$']
path("<album_id>/fav/", views.fav, name="fav"),
This URL needs the album_id. Something like this:
{% url 'music:fav' 1 %}
{% url 'music:fav' album.id %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With