【发布时间】:2020-12-13 16:20:47
【问题描述】:
我是 Django 的新手,我正在开发一个 Web 应用程序项目。这个特定页面应该编辑条目并保存条目。但我不断收到缺少的 1 个必需参数
Views.py
# editPage forms
class editform(forms.Form):
content = forms.CharField(widget=forms.Textarea(), label='')
def edit(request, entry):
if request.method == 'GET':
page = util.get_entry(entry)
return render(request,"encyclopedia/edit.html",{
"form":SearchEntry(),
"edit":editform(initial={'content': page}),
"entry":entry
})
#If this is a POST request
else:
form = editform(request.POST)
if form.is_valid():
content = form.cleaned_data["content"]
util.save_entry(entry,content)
page = util.get_entry(entry)
page = mark.convert(page)
return render(request,"encyclopedia/entry.html",{
"form":SearchEntry(),
"page":page,
"entry": title
})
urls.py
从 django.urls 导入路径
从 .导入视图
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:entry>", views.entry, name="entry"),
path("search", views.search, name="search"),
path("newEntry", views.newEntry, name="newEntry"),
path("edit", views.edit, name="edit"),
编辑 HTML
{% extends "encyclopedia/layout.html" %}
{% block title %}
Edit {{name}}
{% endblock %}
{% block body %}
<h1>{{title}}</h1>
<form action= "{% url 'edit' %}" method="POST">
{% csrf_token %}
{{ edit }}
<br>
<input class="save btn btn-info" type="submit" value="save"/>
</form>
<p> Click the "save" button to save your entry to the encyclopedia.</p>
<br>
<a href = "{% url 'index' %}"> Return Home</a>
{% endblock %}
入口 HTML
{% extends "encyclopedia/layout.html" %}
{% block title %}
Encyclopedia
{% endblock %}
{% block body %}
<h1>{{title}}</h1>
{{entry | safe}}
<a href = "{% url 'edit' %}"> Edit Content</a>
<br>
<br>
<a href = "{% url 'index' %}"> Return Home</a>
{% endblock %}
当我更改此特定网址时:
path("edit/<str:entry>", views.edit, name="edit"),
我遇到了一个不同的问题: 未找到任何参数的“编辑”反向。尝试了 1 种模式:['edit/(?P[^/]+)$']
【问题讨论】:
-
由于您的编辑视图需要一个名为
entry的强制 参数,因此您应该更新url标记用法。