【发布时间】:2021-06-30 00:25:27
【问题描述】:
所以我在做 cs50x 网络开发,任务是创建一个维基百科页面。我创建了一个视图功能,我可以在其中单击编辑页面来编辑内容。但是我一直遇到这个问题。enter image description here
这是来自views.py的代码
class EditForm(forms.Form):
body = forms.CharField(label= "Body")
def edit_page(request):
title = request.POST.get("edit")
body = util.get_entry(title)
form = EditForm(initial={'body': body})
if request.method == "POST":
form = EditForm(request.POST)
if form.is_valid():
content = form.cleaned_data["body"]
util.save_entry(title, content)
return render(request, "encyclopedia/title.html", {
"title": title, "content": util.get_entry(title)
})
else:
return render(request, "encyclopedia/edit.html", {
"form": EditForm()
})
urls.py:
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:title>", views.title, name="title"),
path("search", views.search, name="search"),
path("new_page", views.new_page, name="new_page"),
path("random", views.randomizer, name="random"),
path("edit", views.edit_page, name="edit")
]
title.html:
<form action= "{% url 'edit' %}", method= "POST">
{% csrf_token %}
<button type="submit" name="edit" value="{{title}}" id="edit">Edit Page</button>
</form>
最后是edit.html
<form action="{% url 'edit' %}" method="POST">
{% csrf_token %}
{{form}}
<input type="submit">
</form>
请有人帮我解决这个问题,谢谢!
【问题讨论】:
-
如果表单无效,您永远不会返回响应。 (如果表单有效,则您已返回响应,但如果为 false,则未执行任何操作)
标签: python html django django-views cs50