你好,我正在尝试添加一个编辑页面到我的django url模式,页面给我一个错误,一旦我点击我的页面上的一个链接,系统崩溃,我从系统中得到一个没有反向匹配的错误。如果我确实将编辑路径与入口路径交换,那么它确实可以工作,但这不是我希望它工作的方式。我希望用户被呈现在第一个入口页面,然后能够点击编辑按钮,以编辑页面。下面是我的观点和一些html页面。
django的新手,并查看了文档,但没有找到任何东西。谢谢你的帮助
下面也是错误:
错误-未找到带参数'('# Django\r\n\r\nDjango是使用Python编写的web框架,它允许设计动态生成HTML的web应用程序。\r\n‘,)’的反向操作。尝试了1个模式:['(?P^/+)$']
urls.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"),
]views.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 %}发布于 2020-09-04 00:51:29
如果两个视图的路径相同,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"),
]注意/前面的斜杠
https://stackoverflow.com/questions/63728109
复制相似问题