开发者

In Django, how do I make sure that my forms keep its initial value after being submitted but returned to the template due to an error?

开发者 https://www.devze.com 2023-01-28 05:04 出处:网络
Let\'s say someone submits a form. It doesn\'t pass is_valid(), and I return them back to the template.

Let's say someone submits a form. It doesn't pass is_valid(), and I return them back to the template.

How do I display the form fields again, without havi开发者_运维知识库ng the person retype their stuff?

I prefer not to use the { initial: } option.


You could implement your view like so:

def showForm(request):
    if request.method == "POST":
        form = MyForm(request.POST)
        if form.is_valid():
            # do stuff on success, like redirection
    else:
        form = MyForm()

    return render_to_response(...,
                              {"form" : form})
    # then in the template, use {{ form.as_table }}, for example
0

精彩评论

暂无评论...
验证码 换一张
取 消