简体   繁体   中英

TemplateSyntaxError at /music/ Invalid block tag on line 6: 'path', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

I wanted to remove a hardcoded urls and used a dynamic coded urls and load it over the server but I am receiving an error as: TemplateSyntaxError at /music/ Invalid block tag on line 6: 'path', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

Here are my code:

index.html:

{% if all_albums %}
    <h3>here are all my albums:</h3>
    <ul>
        {% for album in all_albums %}
        <li><a href = "{% path 'music:detail' album.id %}"> {{ album.album_title }}</a></li>
        {% endfor %}
    </ul>
{% else %}
    <h3>You don't have any Albums</h3>
{% endif %}

music.urls.py:

from django.urls import path
from . import views

app_name = 'music'

urlpatterns = [
    # /music/
    path('', views.index, name='index'),
    # /music/712/
    path('<int:album_id>/', views.detail, name='detail'),
]

music.views.py:

from django.shortcuts import render, get_object_or_404
from .models import Album

# noinspection PyUnusedLocal

def index(request):
    all_albums = Album.objects.all()
    return render(request, 'music/index.html', {'all_albums': all_albums})

# noinspection PyUnusedLocal

def detail(request, album_id):
    album = get_object_or_404(Album, pk=album_id)
    return render(request, 'music/detail.html', {'album': album})

Analytic_practice.urls:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('music/', include('music.urls')),
]

try this :

<li><a href = "{% url 'music:detail' album.id %}"> {{ album.album_title }}</a></li>

instead of this :

<li><a href = "{% path 'music:detail' album.id %}"> {{ album.album_title }}</a></li>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM