Home > Mobile >  why form.is_valid() is always false?
why form.is_valid() is always false?

Time:12-05

I tried to create a contact us form in django but i got always false when i want to use .is_valid() function.

this is my form:

from django import forms
from django.core import validators


class ContactForm(forms.Form):
    first_name = forms.CharField(
        widget=forms.TextInput(
            attrs={'placeholder': 'نام خود را وارد کنید'}),
        label="نام ",
        validators=[
            validators.MaxLengthValidator(100, "نام شما نمیتواند بیش از 100 کاراکتر باشد")])

    last_name = forms.CharField(
        widget=forms.TextInput(
            attrs={'placeholder': 'نام خانوادگی خود را وارد کنید'}),
        label="نام خانوادگی",
        validators=[
            validators.MaxLengthValidator(100, "نام خانوادگی شما نمیتواند بیش از 100 کاراکتر باشد")])

    email = forms.EmailField(
        widget=forms.EmailInput(
            attrs={'placeholder': 'ایمیل خود را وارد کنید'}),
        label="ایمیل",
        validators=[
            validators.MaxLengthValidator(200, "تعداد کاراکترهایایمیل شما نمیتواند بیش از ۲۰۰ کاراکتر باشد.")
        ])

    title = forms.CharField(
        widget=forms.TextInput(
            attrs={'placeholder': 'عنوان پیام خود را وارد کنید'}),
        label="عنوان",
        validators=[
            validators.MaxLengthValidator(250, "تعداد کاراکترهای  شما نمیتواند بیش از 250 کاراکتر باشد.")
        ])

    text = forms.CharField(
        widget=forms.Textarea(
            attrs={'placeholder': 'متن پیام خود را وارد کنید'}),
        label="متن پیام",
    )

    def __init__(self, *args, **kwargs):
        super(ContactForm, self).__init__()
        for visible in self.visible_fields():
            visible.field.widget.attrs['class'] = 'form_field require'

this is my view:

from django.shortcuts import render

from .forms import ContactForm
from .models import ContactUs


def contact_us(request):
    contact_form = ContactForm(request.POST or None)
    if contact_form.is_valid():
        first_name = contact_form.cleaned_data.get('first_name')
        last_name = contact_form.cleaned_data.get('last_name')
        email = contact_form.cleaned_data.get('email')
        title = contact_form.cleaned_data.get('title')
        text = contact_form.cleaned_data.get('text')

        ContactUs.objects.create(first_name=first_name, last_name=last_name, email=email, title=title, text=text)
        # todo: show user success message

        contact_form = ContactForm()
    context = {
        'form': contact_form
    }

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

** this is the codes in template**

  <form action="{% url 'contact' %}" id="contactform" method="post">
                                {% csrf_token %}
                                <div >
                                    <div >
                                        {{ form.first_name }}
                                        {% for error in form.first_name.errors %}
                                            <p >{{ error }}</p>
                                        {% endfor %}
                                    </div>
                                </div>
                                <div >
                                    <div >
                                        {{ form.last_name }}
                                        {% for error in form.last_name.errors %}
                                            <p >{{ error }}</p>
                                        {% endfor %}
                                    </div>
                                </div>
                                <div >
                                    <div >
                                        {{ form.email }}
                                        {% for error in form.email.errors %}
                                            <p >{{ error }}</p>
                                        {% endfor %}
                                    </div>
                                </div>
                                <div >
                                    <div >
                                        {{ form.title }}
                                        {% for error in form.title.errors %}
                                            <p >{{ error }}</p>
                                        {% endfor %}
                                    </div>
                                </div>
                                <div >
                                    <div >
                                        {{ form.text }}
                                        {% for error in form.text.errors %}
                                            <p >{{ error }}</p>
                                        {% endfor %}

                                        <div ></div>
                                    </div>
                                </div>
                                <div >
                                    <div >
                                        <button type="submit" 
                                                data-type="contact">ارسال
                                        </button>
                                    </div>
                                </div>
                            </form>

CodePudding user response:

you can use CBVs to easily save data on form valid

views.py

from django.views import generic

class ContactCreateView(generic.CreateView,):
    model = Contact
    fields = "__all__"   
    success_url = reverse_lazy(url_name)

then in in your templates

templates/contact_form.html


<form action="{% url 'contact' %}" id="contactform" method="post">
    {% csrf_token %}
  {% for formfield in form %}
   <div >
       <div >
           {{ formfield}}
           {% for error in formfield.errors %}
               <p >{{ error }}</p>
           {% endfor %}
       </div>
     </div>
#add submit button here

urls.py just remember to add as_view() when calling your view in url

    path("", views.ContactCreateView.as_view(), name="contact"),

or using FBV you could try:

def contact_us(request):
    contact_form = ContactForm(request.POST or None)
    if contact_form.is_valid():
        contact_form.save()
       
        
    context = {
        'form': contact_form
    }

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


CodePudding user response:

Remove these lines from your forms.py:

    def __init__(self, *args, **kwargs):
        super(ContactForm, self).__init__()
        for visible in self.visible_fields():
            visible.field.widget.attrs['class'] = 'form_field require'

Fix your view based on documentation:

def contact_us(request):
    if request.method == 'POST':
        contact_form = ContactForm(request.POST)
        if contact_form.is_valid():
            ContactUs.objects.create(**contact_form.cleaned_data)
            messages.success(request, 'My success message')
            # you want to send to your home
            return redirect('/contact')

    else:
        contact_form = ContactForm()
    context = {
        'form': contact_form
    }

    return render(request, 'contact_us.html', context)

I took the liberty to clean up your view by using **kwargs since you have many fields. Used Django messages to display successful message on obj creation. Here is how to display it.

  • Related