如何在表单验证后保留输入值?

时间:2015-07-29 06:13:23

标签: django django-forms django-templates django-views

用户填写表单但错过了必填字段,点击提交。表单显示错误,并返回'请填写所有字段'。但是表单会清除所有输入值。

如何在表格验证后保留用户的输入值。

contact.html

<div class="row">
    <div class="col-lg-8 col-lg-offset-2 text-center">
        <form method="POST" action='{% url "contact" %}' role="form" id="contact-form" class="contact-form"> {% csrf_token %}
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <input type="text" class="form-control" name="name" id="name" placeholder="Name*">
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <input type="email" class="form-control" name="email" id="email" placeholder="E-mail*">
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <input type="text" class="form-control" name="contact_number" id="contact_number" placeholder="Contact Number">
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <input type="select" class="form-control" name="subject" id="subject" placeholder="Subject">
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        <textarea class="form-control textarea" rows="3" name="message" id="message" placeholder="Message*"></textarea>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <button type="submit" class="btn main-btn pull-right">Send</button>
                </div>
          </div>
        </form>
    </div>
</div>

views.py

def contact(request):
    contact_us_form = ContactUsForm(request.POST or None)
    name = request.POST.get('name', '')
    email = request.POST.get('email', '')
    subject = request.POST.get('subject', '')
    contact_number = request.POST.get('contact_number', '')
    message = request.POST.get('message', '')
    context = {
        "contact_us_form": contact_us_form,
        "name": name,
        "email": email,
        "contact_number": contact_number,
        "subject": subject,
        "message": message,
        }

    if request.method == "POST":
        if contact_us_form.is_valid():
            new_query = contact_us_form.save()
            messages.success(request, "Thank you, your message has been successfully sent. We'll get back to you soon.")
            return HttpResponseRedirect("/")
        else:
            messages.error(request, "* fields are required.")

    return render(request, 'contact/contact.html', context)

models.py

class ContactUs(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField(max_length=120)
    contact_number = models.CharField(max_length=100, null=True, blank=True)
    subject = models.CharField(max_length=120, null=True, blank=True)
    message = models.TextField(max_length=850)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)

    def __unicode__(self):
        return self.name

0 个答案:

没有答案