NoReverseMatch 在/在 django

NoReverseMatch at / in django

大家好,我想继续查看详细信息,但是在粘贴 url

时出现此错误
NoReverseMatch at /
Reverse for 'test' with no arguments not found. 1 pattern(s) tried: ['test/(?P<pk>[0-9]+)/$']

这是我的Models.py

from django.db import models

# Create your models here.
class article(models.Model):
    Title = models.CharField(max_length=200)
    Content = models.CharField(max_length=1000)

这是我的Views.py

from django.shortcuts import render
from django.views.generic import (ListView,DetailView)
from .models import *
# Create your views here.

class Article(ListView):
    model = article

class Test(DetailView):
    model = article

这是我的Urls.py

from django.contrib import admin
from django.urls import path
from cbs import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.Article.as_view(),name="Article"),
    path('test/<int:pk>/', views.Test.as_view(),name="test"),
]

这是我的Article_list.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    {% for article in article_list %}
      <h1><a href="{% url 'test' %}">{{ article.Title }}</a></h1>
    {% endfor %}
  </body>
</html>

在您的 Article_list.html 文件中,您必须将标签上的 href 属性更改为:

    <h1><a href="{% url 'test' article.id %}">{{ article.Title }}</a></h1>

将 id 作为参数发送到下面 url:

    path('test/<int:pk>/', views.Test.as_view(),name="test"),