python - Django NoReverseMatch at / 路径名和/或顺序是否冲突?
问题描述
您好,我正在尝试向我的 django url 模式添加一个编辑页面,一旦我单击页面上的一个链接,系统就会崩溃,并且系统会显示没有反向匹配错误,该页面会给我一个错误。如果我确实将编辑路径与输入路径交换,那么它确实可以工作,但这不是我希望它工作的方式。我希望用户首先看到入口页面,然后能够单击编辑按钮来编辑页面。以下是我的观点和一些 html 页面。
django 的新手并查看了文档,但没有找到任何东西。感谢您的帮助
下面也是错误:
错误 - 使用参数 '('# Django\r\n\r\nDjango 是一个使用Python编写的 Web 框架,允许设计动态生成HTML的 Web 应用程序。\r\n',)'未找到。尝试了 1 种模式:['(?P[^/]+)$']
网址.py:
from django.urls import path
from . import views
app_name = "enc"
urlpatterns = [
path("", views.index, name="index"),
path("new_page", views.new_page,name="check_page"),
path("",views.get_search,name="search"),
path("<str:title>",views.entry, name="page"),
path("<str:title>",views.edit, name ="edit"),
]
视图.py
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from . import util
entries = {}
def index(request):
return render(request, "encyclopedia/index.html", {
"entries": util.list_entries()
})
def entry(request, title):
if util.get_entry(title):
return render(request,"encyclopedia/entry.html", {
"title": util.get_entry(title),
"name":title
})
else:
return render(request, "encyclopedia/error.html")
def get_search(request):
if request.method == 'GET':
form = util.search_form(request.GET['q'])
if form.is_valid():
search = form.cleaned_data["search"]
return render(request, "encyclopedia/entry.html",{
"search":util.get_entry(search)
})
def new_page (request):
if request.method == 'POST':
form = util.new_entry(request.POST)
if form.is_valid():
title = form.cleaned_data['title']
details = form.cleaned_data['details']
if title in entries:
return render (request, "encyclopedia/error.html")
else:
util.save_entry(title, details)
entries[title] = details
return HttpResponseRedirect(f'{title}')
return render(request, "encyclopedia/new_page.html",{
"form": util.new_entry()
})
def edit (request,title):
entry = util.get_entry(title)
if entry == None:
return render(request, "encyclopedia/error.html")
else:
form = util.edit(initial={'title':title, 'details':entry})
return render (request, "encyclopedia/edit.html", {
"form": form,
'title': title,
'details':util.get_entry(title),
} )
用于编辑的html:
{% extends "encyclopedia/layout.html" %}
{% block title %}
Encyclopedia
{% endblock %}
{% block body %}
<form action="{% url 'enc:edit' title %}" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit">
</form>
{% endblock %}
用于输入的html
{% block title %}
{{name}}
{% endblock %}
{% block body %}
<p>{{title}}</p>
<a href="{% url 'enc:index' %}">Go back to main page</a>
<a href="{% url 'enc:edit' title %}">Click here to edit the page</a>
{% endblock %}
解决方案
如果两个视图的路径相同,Django 会抛出错误。试试这个来解决问题。
urlpatterns = [
path("", views.index, name="index"),
path("new_page/", views.new_page,name="check_page"),
path("search/",views.get_search,name="search"),
path("new/<str:title>/",views.entry, name="page"),
path("edit/<str:title>/",views.edit, name ="edit"),
]
并小心 / 正斜杠
推荐阅读
- python - CLI 中 timeit 输出的解释
- mysql - datetime MySQL 解析日期
- reactjs - 反应钩子,通过传递值选择
- css - 我可以在 laravel 中为 2 个部分使用 2 个引导程序吗?
- automation - g1ant:未实现方法长度()的jsonpath
- arrays - 将假定等级实体写入文件
- php - 重新启动时未创建 Php7.2-fpm.sock。使用 nginx 在 raspberrypi 上重新启动 php7.2-fpm 无法正常工作
- python - read_csv 将所有输入读入某些行的第一列
- sql - 给定以下数据,编写查询以生成表的最佳方法是什么?
- javascript - 在开始下一个函数之前等待一个函数的结束