【发布时间】:2021-02-05 23:45:12
【问题描述】:
我正在尝试构建一个 wiki 页面,已经搜索过其他帖子但无法解决我的问题,有人可以帮忙吗? 我收到以下错误:Reverse for 'edit' with no arguments not found。尝试了 1 种模式:['wiki/edit/(?P[^/]+)/$']
基本上它是一个编辑 wiki 条目的编辑页面,编辑条目的按钮位于 entry.html,这应该将条目名称作为参数发送到 edit.url 然后我编辑它并通过 POST 发送新内容以覆盖条目的实际内容。
这是我的意见.py
def edit(request, entry):
if request.method == 'POST':
content = request.POST.get('edit')
util.save_entry(entry, content)
return render(request, "encyclopedia/entry.html", {
"entry": markdown2.markdown(util.get_entry(entry)),
"title": entry
})
else :
return render(request, "encyclopedia/edit.html", {
"entry": entry
})
这是我的 urls.py
from django.urls import path, re_path
from . import views
app_name = "encyclopedia"
urlpatterns = [
path("", views.redirect, name="redirect"),
path("wiki", views.index, name="index"),
path("wiki/search/", views.search, name="search"),
path("wiki/newentry", views.new, name="new"),
path("wiki/edit/<str:entry>/", views.edit, name="edit"),
path("wiki/<str:entry>", views.entry, name="entry")
]
还有我的edit.html
{% extends 'encyclopedia/layout.html' %}
{% block title %}
Edit Page
{% endblock %}
{% block body %}
<form action="{% url 'encyclopedia:edit' %}" method="POST">
{% csrf_token %}
<label for="edit">Edit {{ entry }}</label>
<textarea id="edit" name="edit" style="display: block; height: 70%;"></textarea>
<button type="submit">Edit</button>
{% endblock %}
忘记放entry.html了,这是edt4it按钮所在的页面,通过GET重定向到edit.html。
{% extends "encyclopedia/layout.html" %}
{% block title %}
{{ title }}
{% endblock %}
{% block body %}
{{ entry|safe }}
<a class="btn btn-primary" href="{% url'encyclopedia:edit'entry=title%}">Edit Entry</a>
{% endblock %}
【问题讨论】:
标签: html python-3.x django