Strange bug with form errors

15 views
Skip to first unread message

Nestor Burma

unread,
Aug 15, 2016, 4:15:37 AM8/15/16
to django...@googlegroups.com
I have a very strange bug with fom errors. On my development site, my code works fine. But on my production site (apache + mod-wgsi + posgresql), the form errors I defined in is_valid() method of my ModelForm (in a generic CreateView) are not product by the template. Have you already seen such a thing ?
 
Here is my code :
 
 
views.py:
class UserCreate(CreateView):
    model = User
    template_name = 'base/userCreate.html'
    success_url = '/utilisateurs/nouveau/success/{id}'
    form_class = UtilisateurCreateForm
 
forms.py:
class UtilisateurCreateForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'email']
    def is_valid(self):
        valid =  super().is_valid()
        if email != '' and User.objects.filter(email = email).exists():
            self.add_error('email', 'Il existe déjà un utilisateur avec cet email.')
            valid = False
        return valid
 
userCreate.html:
{% extends "base/base.html" %}
{% block contenu %}
<h3>Nouvel utilisateur :</h3>
<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Enregistrer" />
</form>
{% endblock %}
 
Thank's for your help.
 

M Hashmi

unread,
Aug 15, 2016, 4:59:39 AM8/15/16
to django...@googlegroups.com
Can you paste the full traceback from production server?

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/trinity-68cd841e-22eb-421b-95ea-2b94ca00a28b-1471243983623%403capp-mailcom-bs11.
For more options, visit https://groups.google.com/d/optout.

Nestor Burma

unread,
Aug 15, 2016, 6:11:22 AM8/15/16
to django...@googlegroups.com
Oh I just have found the trick !
It comes from a django bug in some earlier version of django 1.9. in FormeMixinBase.form_invalid()
 
I had:
    def form_invalid(self, form):
        return self.render_to_response(self.get_context_data())
 
in place of:
    def form_invalid(self, form):
        return self.render_to_response(self.get_context_data(form=form))
 
so parameters wasn't transfered. Probably my pip did'nt install last version, and my production version was not exactly the same as my development one... An upgrade solved the problem.
 
Ouf ! I believed I was going to come crazy !
 
Thank's for your answer.
 
Envoyé: lundi 15 août 2016 à 08:59
De: "M Hashmi" <mhash...@gmail.com>
À: django...@googlegroups.com
Objet: Re: Strange bug with form errors
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

 

--

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 post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

M Hashmi

unread,
Aug 15, 2016, 7:31:17 AM8/15/16
to django...@googlegroups.com
Ok great.

Take care.

On Mon, Aug 15, 2016 at 3:10 AM, Nestor Burma <nest...@caramail.fr> wrote:
Oh I just have found the trick !
It comes from a django bug in some earlier version of django 1.9. in FormeMixinBase.form_invalid()
 
I had:
    def form_invalid(self, form):
        return self.render_to_response(self.get_context_data())
 
in place of:
    def form_invalid(self, form):
        return self.render_to_response(self.get_context_data(form=form))
 
so parameters wasn't transfered. Probably my pip did'nt install last version, and my production version was not exactly the same as my development one... An upgrade solved the problem.
 
Ouf ! I believed I was going to come crazy !
 
Thank's for your answer.
 
Envoyé: lundi 15 août 2016 à 08:59
De: "M Hashmi" <mhash...@gmail.com>
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

 

--
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+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

--
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+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages