简体   繁体   中英

remembering form field value on django model form

I am quite new to Django as such, and have been playing around with ModelForms. So far, I have been able to create ModelForms with ease. However, one problem seems to bug me a bit:

When a user fills the form, and if there is an error (say int instead of char or a missing blank=False value), the form spits out the error and seems to forget the values the user entered when the form failed to validate. I am wondering if there is a way to remember these values so that the user does not have to enter them again.

At the moment, I have something like the following:

class ContactForm(CreateView):

    form_class = ContactForm
    template_name = "superform.html"
    success_url = '/ty/'

    def form_valid(self, form):
        # do something useful with validated form
        return super(ContactForm, self).form_valid(form)

    def form_invalid(self, form):
        # do something useful with invalidated form.
        return super(ContactForm,self).form_invalid(form)

I am assuming I need to do something in form_invalid to pass the values back to the form - but I am unsure how to do this(?).

I would really appreciate if someone could point me in the right direction.

Thanks.

-- --Edit---

            <form class="form" role="form" action="{% url 'coolurl' %}" method="post">{% csrf_token %}

                    {% if form.non_field_errors %}
                      <div class="alert">
                      {% for error in form.non_field_errors %}
                      <span class="label">Error: {{ error|escape }}</span> 
                      {% endfor %}
                      </div>
                    {% endif %}


                    <div class="form-group">
                        {% if form.name.errors %}
                          <div class="alert">
                          {% for error in form.name.errors %}
                          <span class="label">Error: {{ error|escape }}</span> 
                          {% endfor %}
                          </div>
                        {% endif %}

                        <label class="col-sm-3" for="id_name">Name:</label>
                        <div class="col-sm-6">
                        <input class="form-control" 
                             id="id_name" 
                             type="text" 
                             name="name" 
                             maxlength="128" 
                             placeholder="Your name..">
                        </div>
                    </div>

                    <button type="submit" class="btn btn-success">Submit</button>
            </form>

Try to mention the value of the input tag:

<input class="form-control" 
                             id="id_name" 
                             type="text" 
                             name="name" 
                             maxlength="128"
                             value="{{ form.name.value|default_if_none:'' }}"
                             placeholder="Your name..">
                        </div>

You've taken the responsibility for rendering those fields away from Django by simply hard-coding them in the HTML. That means that Django has no way of inserting the current values; not only when redisplaying after errors, but also if you have a form that modifies existing database content.

Don't do it like that. I understand that you don't want to just juse form.as_p , but there is a good middle ground: output each field in the template with {{ form.my_field }} . You can add relevant classes to the fields in the definition in forms.py, and then Django will take care of outputting it correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM