【问题标题】:NoReverseMatch at / in djangoNoReverseMatch at / in django
【发布时间】:2020-02-28 21:15:23
【问题描述】:

大家好,我想继续查看详细信息,但粘贴网址时出现此错误

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>

【问题讨论】:

    标签: django django-models django-templates django-views django-urls


    【解决方案1】:

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

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

    将 id 作为参数发送到以下网址:

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

    【讨论】:

      猜你喜欢
      • 2020-06-01
      • 2014-12-22
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      相关资源
      最近更新 更多