提交失败后如何保存django中的表单字段? [英] How to preserve form fields in django after unsuccessful submit?

查看:126
本文介绍了提交失败后如何保存django中的表单字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自views.py的代码:

  def feedback(request):
if request.method == POST:
form = CommentForm(request.POST)
如果form.is_valid():
form.save()
else:
print(form错误:,form.errors)
else:
form = CommentForm()
articles = Comment.objects.all()
ResponseDict = {articles:articles,形式:form}
return render_to_response(feedback.html,ResponseDict,
context_instance = RequestContext(request))

我已经尝试过这个和几个修改,从类似的问题的答案,但没有任何作用。当我按提交按钮时,html中的所有表单字段都将变为空白。



编辑:feedback.html的代码:

  {%extendsbase.html%} 
{%block main%}
< table>
< form action =/ feedback /method =POST>
{%csrf_token%}
< div class =article>
< label for =name>
Вашеимя:
< / label>
< br />
< input type =textname =nameid =namesize =40class =inputboxvalue =/>
< br />
<! - class =inputbox required - >
< textarea class =WithoutTinymcecols =50rows =10name =textid =text>< / textarea>
< br />
< input type =submitname =submitvalue =Отправить>
< / div> <! - / article - >
< / form>
< / table>
{%includearticles.html%}
{%endblock%}

如果需要,我也可以从base.html粘贴代码。



EDIT2:从base.html最小化的代码:

 < html xmlns =http://www.w3.org/1999/xhtmlxml:lang =cslang =cs> 
...
< body id =body-idonload =loaded()>

<! - 主 - >
< div id =mainclass =box>
< div id =pageclass =box>
< div id =page-inclass =box>
<! - 内容 - >
< div id =content>
{%block main%}
{%endblock%}
< hr class =noscreen/>
< / div> <! - / content - >
< / div> <! - / page-in - >
< / div> <! - / page - >
< / div> <! - / Main - >
< / body>
< / html>


解决方案

在您的模板中,您没有使用<$ c / c code code code code code code code code $ c

first_field second_field

 < form action =/ feedback /method =POST> 
{%csrf_token%}
< div class =article>
< label for =name>
Вашеимя:
< / label>
< br />
{{form.first_field.errors}}
{{form.first_field.label_tag}}:{{form.first_field}}
< br />
<! - class =inputbox required - >
{{form.second_field.errors}}
{{form.second_field.label_tag}}:{{form.second_field}}
< br />
< input type =提交nam e =submitvalue =Отправить>
< / div> <! - / article - >
< / form>

有关更多参考资料 - 使用模板显示表单


Code from views.py:

def feedback(request):
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            form.save()
        else:
            print("form.errors:", form.errors)
    else:
        form = CommentForm()
    articles = Comment.objects.all()
    ResponseDict = {"articles": articles, "form": form}
    return render_to_response("feedback.html", ResponseDict, 
        context_instance = RequestContext(request))

I've tried this and several modifications from answers to similar questions, but nothing works. When I press submit button, all form fields in html become empty.

EDIT: code from feedback.html:

{% extends "base.html" %}
{% block main %}
    <table>
        <form action="/feedback/" method="POST">
            {% csrf_token %}
            <div class="article">
                <label for="name">
                    Ваше имя:
                </label>
                <br />
                <input type="text" name="name" id="name" size="40" class="inputbox" value="" />
                <br />
                <!-- class="inputbox required" -->
                <textarea class="WithoutTinymce" cols="50" rows="10" name="text" id="text"></textarea>
                <br />
                <input type="submit" name="submit" value="Отправить">
            </div> <!-- /article -->
        </form>
    </table>
    {% include "articles.html" %}
{% endblock %}

I can also paste code from base.html if needed.

EDIT2: minimized code from base.html:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
...
<body id="body-id" onload="loaded()">

<!-- Main -->
<div id="main" class="box">
    <div id="page" class="box">
        <div id="page-in" class="box">
            <!-- Content -->
            <div id="content">
                {% block main %}
                {% endblock %}
                <hr class="noscreen" />
            </div> <!-- /content -->
        </div> <!-- /page-in -->
    </div> <!-- /page -->
</div> <!-- /Main -->
</body>
</html>

解决方案

In your template you are not making use of form passed by the view.

You can update part of your template as (assuming your field names in the form are first_field and second_field.

<form action="/feedback/" method="POST">
        {% csrf_token %}
        <div class="article">
            <label for="name">
                Ваше имя:
            </label>
            <br />
            {{ form.first_field.errors }}
            {{ form.first_field.label_tag }}: {{ form.first_field }}
            <br />
            <!-- class="inputbox required" -->
            {{ form.second_field.errors }}
            {{ form.second_field.label_tag }}: {{ form.second_field }}
            <br />
            <input type="submit" name="submit" value="Отправить">
        </div> <!-- /article -->
    </form>

For more reference - Displaying form using template

这篇关于提交失败后如何保存django中的表单字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆