How remove "__all__" from the beginnings of form error messages?

1,555 views
Skip to first unread message

Christian Seberino

unread,
Aug 3, 2020, 9:22:10 AM8/3/20
to Django users
I created a custom form error messag like so...

     raise django.forms.ValidationError("Invalid date")

However, when I do {{form.errors}} in a template I see this....

    __all__
  • Invalid date


  • How remove that "__all__" part at beginning?

oba stephen

unread,
Aug 3, 2020, 12:06:10 PM8/3/20
to django...@googlegroups.com
Hi, could you share your forms.py, your views.py and the template.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/be4065bb-2f54-4275-a216-a1767e1dc1d5o%40googlegroups.com.

Christian Seberino

unread,
Aug 3, 2020, 1:46:53 PM8/3/20
to Django users
oba

Thanks.  Here are all the files you asked for.......

=============================================================
forms.py:
=============================================================

class AppointmentForm(django.forms.Form):
        customer = django.forms.IntegerField()
        year     = django.forms.CharField(min_length = 4, max_length = 4)
        month    = django.forms.CharField(max_length = 2)
        day      = django.forms.CharField(max_length = 2)
        hour     = django.forms.CharField(max_length = 2,
                                          validators = [verify_hour])
        minute   = django.forms.CharField(min_length = 2, max_length = 2)
        am_or_pm = django.forms.CharField(max_length = 2,
                                          widget     = AM_OR_PM,
                                          initial    = "PM")

        def clean(self):
                cleaned_data = super().clean()
                try:
                        year   = int(cleaned_data.get("year"))
                        month  = int(cleaned_data.get("month"))
                        day    = int(cleaned_data.get("day"))
                        date   = datetime.date(year  = year,
                                               month = month,
                                               day   = day)
                except (ValueError, TypeError):
                        raise django.forms.ValidationError("Invalid date")
                if date < datetime.date.today():
                        raise django.forms.ValidationError("Invalid date")
                try:
                        hour   = int(cleaned_data.get("hour"))
                        minute = int(cleaned_data.get("minute"))
                        datetime.time(hour = hour, minute = minute)
                except (ValueError, TypeError):
                        raise django.forms.ValidationError("Invalid time")


=============================================================
view
=============================================================

CUST   = grandmas4hire.models.Customer
APPT   = grandmas4hire.models.Appointment

...

@django.contrib.auth.decorators.login_required
def admin_appt(request):
        custs = CUST.objects.all()
        custs = [(e.first + " " + e.last, e.id) for e in custs]
        appts = [e for e in APPT.objects.all()]
        appts = sorted(appts,
                       key = lambda a : a.customer.last + a.customer.first +   \
                                                    str(a.date) + str(a.time))
        if request.method == "POST":
                form = grandmas4hire.forms.AppointmentForm(request.POST)
                if form.is_valid():
                        cust  = form.cleaned_data["customer"]
                        cust  = CUST.objects.get(id = cust)
                        year  = int(form.cleaned_data["year"])
                        month = int(form.cleaned_data["month"])
                        day   = int(form.cleaned_data["day"])
                        date  = datetime.date(year  = year,
                                              month = month,
                                              day   = day)
                        hour  = int(form.cleaned_data["hour"])
                        min_  = int(form.cleaned_data["minute"])
                        time  = datetime.time(hour = hour, minute = min_)
                        if form.cleaned_data["am_or_pm"] == "PM":
                                time = datetime.time(hour   = time.hour + 12,
                                                     minute = time.minute)
                        appt  = APPT(customer = cust,
                                     user     = request.user,
                                     date     = date,
                                     time     = time,
                                     status   = "in process",
                                     reminded = False,
                                     updated  = False)
                        appt.save()
                        reply = django.shortcuts.redirect("/admin_appt")
                else:
                        reply = django.shortcuts.render(request,
                                                        "admin_appt.html",
                                                        {"form"  : form,
                                                         "custs" : custs,
                                                         "appts" : appts})
        else:
                form  = grandmas4hire.forms.AppointmentForm()
                reply = django.shortcuts.render(request,
                                                "admin_appt.html",
                                                {"form"  : form,
                                                 "custs" : custs,
                                                 "appts" : appts})

        return reply

=============================================================
template
=============================================================

{% extends "html_base" %}
{% block body_elements %}

<div id = "admin_appt">
        <form action = "." method = "post"> {% csrf_token %}
                <p>ADD APPOINTMENTS {{form.errors}}</p>

                <div>Customer:</div>
                <p>
                        <select name = "customer" id = "id_customer">
                                {% for cust in custs %}
                                        <option value = {{cust.1}}>
                                                {{cust.0}}
                                        </option>
                                {% endfor %}
                        </select>
                </p>

                <div>Date (YYYY-MM-DD):</div>
                <p>{{form.year}}-{{form.month}}-{{form.day}}</p>

                <div>Time (HH:MM): {{form.hour.errors}}</div>
                <p>{{form.hour}}:{{form.minute}} {{form.am_or_pm}}</p>

                <p><input type = "submit" value = "ADD APPOINTMENT"/></p>
        </form>

        <p><a href = "/admin">Go Back To Admin Page</a></p>

        <table>
                <tr>
                        <th>NAME</th>
                        <th>DATE</th>
                        <th>TIME</th>
                </tr>

                {% for appt in appts %}
                        <tr>
                                <td>
                                        {{appt.customer.first}}
                                        {{appt.customer.last}}
                                </td>
                                <td>{{appt.date|date:"Y-m-d"}}</td>
                                <td>{{appt.time|time:"h:i A"}}</td>
                        </tr>
                {% endfor %}
        </table>
</div>

{% endblock %}


Christian Seberino

unread,
Aug 3, 2020, 2:12:16 PM8/3/20
to Django users
I figured out the problem...If you only want the "global" errors to show up at the top of the form
then you need to use {{form.non_field_errors}} instead of {{form.errors}}.


oba stephen

unread,
Aug 3, 2020, 2:31:56 PM8/3/20
to django...@googlegroups.com
Alright, Nice to know.

Cheers.

On Mon, Aug 3, 2020 at 7:12 PM Christian Seberino <cseb...@gmail.com> wrote:
I figured out the problem...If you only want the "global" errors to show up at the top of the form
then you need to use {{form.non_field_errors}} instead of {{form.errors}}.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages