将pk添加到url地址时出现NoReverseMatch错误



我是编程新手,在urls模块中将主键添加到url地址后会出现NoReverseMatch错误。

设置.py

from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES_DIR=os.path.join(BASE_DIR,"templates")
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-6mtsgnhk=-=5ka0-e4619v0_uq)s+t=dtybe8^ojwmg1gwoj**'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []

# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'g7app',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'g7.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'g7.wsgi.application'

# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}

urls.py

from django.contrib import admin
from django.urls import path
from g7app.views import CreatePost,View1,View2,EditPost,DeleteView,CreatePost
urlpatterns = [
path('admin/', admin.site.urls),
path("",View1.as_view(),name="home"),
path("post/<int:pk>/",View2.as_view(),name="post_detail"),
path("post/<int:pk>/post_edit/",EditPost.as_view(),name="post_edit"),
path("post/<int:pk>/post_delete/",DeleteView.as_view(),name="post_delete"),
path("post/new/",CreatePost.as_view(),name="post_new"),
]

admin.py

from django.contrib import admin
from .models import Student
# Register your models here.
admin.site.register(Student)

型号.py

from django.db import models
from django.urls import reverse

# Create your models here.
class Student(models.Model):
name=models.CharField(max_length=100)
description=models.CharField(max_length=100)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("post_detail", args=[self.pk])

视图.py

from django.shortcuts import render
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView  # new
from django.urls import reverse_lazy  # new
from .models import Student
# Create your views here.
class View1(ListView):
context_object_name="con1"
template_name="home.html"
model=Student
class View2(DetailView):
context_object_name="con2"
template_name="post_detail.html"
model=Student
class CreatePost(CreateView):
fields=["name","description"]
template_name="create_post.html"
model=Student
class EditPost(UpdateView):
fields=["description"]
template_name="edit_post.html"
model=Student
class DeleteView(DeleteView):
template_name="delete_post.html"
model=Student
success_url=reverse_lazy("home")

base.html

<!DOCTYPE HTML>
<HTML>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Blog Application</h1>
{% block c1 %}
<h2><a href="{% url 'post_new' %}">+Create_Post</a></h2>
{% endblock c1 %}
</body>
</html>

home.html

<!DOCTYPE HTML>
{% extends "base.html" %}
{% block c1 %}
{% for Student in con1 %}
<h2><a href="{% url 'post_detail' Student.pk %}">{{Student.name}}</a></h2>
<h4>{{Student.description}}</h4>

{% endfor %}
{% endblock c1 %}

post_detail.html

{% extends "base.html" %}
{% block c1 %}
<div>
<h2>{{con2.name}}</h2>
<h4>{{con2.description}}</h4>
</div>
<p><a href="{% url 'post_edit' Student.pk %}">Edit post</a></p>
<p><a href="{% url 'post_delete' Student.pk %}">Delete post</a></p>
{% endblock c1 %}

create_post.html

<!DOCTYPE HTML>
<HTML>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
{% extends "base.html" %}
{% block c1 %}
<form method="post">
{csrf_token}
{{form.as_p}}
<input type="submit" value="save">
</form>
{% endblock c1 %}
</body>
</html>

edit_post.html

{% extends "base.html" %}
{% block c1 %}
<div>
<h3>edit information</h3>
<form method="post">
{csrf_token}
{{form.as_p}}
<input type="submit" value="save">
</form>
</div>
{% endblock c1 %}

delete_post.html

{% extends "base.html" %}
{% block c1 %}
<div>

<h3>delet post</h3>
<form method="post">
{csrf_token}
{{form.as_p}}
<input type="submit" value="save">
</form>
</div>
{% endblock c1 %}

编辑您的urls.py文件:

from django.contrib import admin
from django.urls import path
from g7app.views import CreatePost,View1,View2,EditPost,DeleteView,CreatePost
app_name = 'my_app' #add this line
urlpatterns = [
path('admin/', admin.site.urls),
path("",View1.as_view(),name="home"),
path("post/<int:pk>/",View2.as_view(),name="post_detail"),
path("post/<int:pk>/post_edit/",EditPost.as_view(),name="post_edit"),
path("post/<int:pk>/post_delete/",DeleteView.as_view(),name="post_delete"),
path("post/new/",CreatePost.as_view(),name="post_new"),
]

编辑你的base.html文件:

<!DOCTYPE HTML>
<HTML>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Blog Application</h1>
{% block c1 %}
<!-- here I added app_name to url -->
<h2><a href="{% url 'my_app:post_new' %}">+Create_Post</a></h2>
{% endblock c1 %}
</body>
</html>

编辑您的主页.html

<!DOCTYPE HTML>
{% extends "base.html" %}
{% block c1 %}
{% for Student in con1 %}
<!-- here I added app_name to url -->
<h2><a href="{% url 'my_app:post_detail' Student.pk %}">{{Student.name}}</a></h2>
<h4>{{Student.description}}</h4>

{% endfor %}
{% endblock c1 %}

编辑您的post_detail.html

{% extends "base.html" %}
{% block c1 %}
<div>
<h2>{{con2.name}}</h2>
<h4>{{con2.description}}</h4>
</div>
<!-- here I added app_name to urls -->
<p><a href="{% url 'my_app:post_edit' Student.pk %}">Edit post</a></p>
<p><a href="{% url 'my_app:post_delete' Student.pk %}">Delete post</a></p>
{% endblock c1 %}

最新更新