Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

python 3.x - django not displaying error_messages in forms

i am new to django and i have this form i am working on i have tried everything possible i cant seem to display the form error messages. i have tried using a def clean method nothing happens when i try to submit an empty form..i have tired switing to using FormView i have also tried using function view i have tried using a for loop and adding {{ form.non_field_errors }} in my template nothing pops up.

my app/forms.py

from django import forms

class ContactForm(forms.Form):

    name = forms.CharField( label='Your Name', min_length=2, max_length=25, required=True, error_messages ={'required':'Please tell Oluwafemi your name'})
    email = forms.EmailField(label='Your Email', required=True, error_messages={'invalid':'Please fill in a valid email'})
    subject = forms.CharField(label='Subject', min_length=4, max_length=100, required=True)
    message = forms.CharField(widget=forms.Textarea(attrs={'placeholder':'Write Oluwafemi a Message'}), error_messages ={'required':'Please write something for Oluwafemi'})

my app/views.py

from django.views.generic import TemplateView
from django.shortcuts import render
from django.core.mail import send_mail
from .forms import ContactForm
from django.http import HttpResponseRedirect

# Create your views here.
class ProfilePageView(TemplateView):
    template_name = 'femi_profile.html'
    form_class = ContactForm
    success_url = 'femiir'

    def get(self, request, *args, **kwargs):
        form = self.form_class()
        return render(request, self.template_name, {'form': form})

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)

        if form.is_valid():
            name = form.cleaned_data['name']
            email = form.cleaned_data['email']
            subject = form.cleaned_data['subject']
            message = form.cleaned_data['message']

            # Send the email
            send_mail(
                f"{subject} - message from {name} ", # message subject
                message, # Message Body
                email, # Sender's mail (From mail)
                ['[email protected]'], # Reciever's mail(To mail)
            )

            return HttpResponseRedirect('femiir')


        return render(request, self.template_name, {'form': form})

snippet of my html template

<div class="col-lg-7 mt-5 mt-lg-0 d-flex align-items-stretch">
            <form method="post">
              {% csrf_token %}
              
              <div class="form-row">
                <div class="form-group col-md-6">
                  {{ form.name|as_crispy_field }}
                </div>
                <div class="form-group col-md-6">
                  {{ form.email|as_crispy_field }}
                </div>
              </div>
              <div class="form-group">
                {{ form.subject|as_crispy_field }}
                {{ form.message|as_crispy_field }}
              </div>
              <div class="text-center"><button type="submit" class="btn btn-primary ">Send Message</button></div>
            </form>
          </div>

please what i my doing wrong

question from:https://stackoverflow.com/questions/65602016/django-not-displaying-error-messages-in-forms

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Did you try looping through form.errors in template?

{% if form.errors %}
    {% for field in form %}
        {% for error in field.errors %}
          {{ error }}  
        {% endfor %}
    {% endfor %}
    {% for error in form.non_field_errors %}
        {{ error }}
    {% endfor %}
{% endif %}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...