【问题标题】:How do i get rid of value error, the view xxx didn't return an HttpResponse Object. It returned none instead我如何摆脱值错误,视图 xxx 没有返回 HttpResponse 对象。它没有返回
【发布时间】: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


【解决方案1】:

form = EditForm(request.POST or None ,initial={'body': body})

【讨论】:

  • 我的代码改成这个后仍然出现同样的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 2017-12-28
  • 1970-01-01
相关资源
最近更新 更多