开发者

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?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜