创建的网址.py文件包含app_name=“articles”,该文件在作为模型模板建立的“模板_base.html”中无法识别。
NoReverseMatch at /articles/ Reverse for 'article' with keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: \ ['articles/(?P\<slug\>\[-a-zA-Z0-9\_\]+)/\\Z'\]
受影响的线路为:
\<h2\>\<a href="{% url 'articles:article' slug=article.slug %}"\>{{ article.titre }}\</a\>\ </h2\>
3次我尝试执行与课程中相同的代码,但我需要您的帮助。
网址.py
from django.contrib import admin from django.urls import path, include from articles import views app_name = "articles" urlpatterns = [ path('', views.articles_view, name='articles'), path('<slug:slug>/', views.article_view, name='article') ]
模板_base.html
<!DOCTYPE html> <html lang="fr"> <head> <meta charset="UTF-8"> <title>{% block titre %}{% endblock %}</title> </head> <body> <ul style="list-style: none"> <li style="display: inline-block; padding: 10px 20px;"> <a href="{% url 'home' %}">Accueuil</a> </li> <li style="display: inline-block; padding: 10px 20px;"> <a href="{% url 'articles:articles' %}">Articles</a> </li> <li style="display: inline-block; padding: 10px 20px;"> <a href="{% url 'contact' %}">Contact</a> </li> </ul> {% block contenu %} {% endblock %} </body> </html>
视图.py
from django.shortcuts import render from django.http import HttpResponse from articles.models import Article def articles_view(request): articles = Article.objects.all() return render(request, 'articles/list.html', context={'articles': articles}) def article_view(request, slug): return HttpResponse("Page d'article")