Simple form not validating

泄露秘密 提交于 2020-01-01 17:57:06

问题


I have found here on stackoverflow a method to extend django's built-in authentication using signals. My base User is defined by 'email' and passwords (so no username there). So I'm trying to modify it to my needs, but I'm geting a validation error for my form. Strange thing is that error is connected to the User.email field and I'm getting 'already in use' even though I'm just registering at the moment. Is it trying to save it 2 times or what ? I've discovered it when I was sending dictionary with data to form's contstructor in shell: form = MyForm(data={}). After this form was still invalid, but changing email to different value finally gave me True.

The user_created function connected to registration signal :

def user_created(sender, user, request, **kwargs):
    form = CustomRegistrationForm(request.POST, request.FILES)
    if form.is_valid():
        data = UserProfile(user=user)
        data.is_active = False
        data.first_name = form.cleaned_data['first_name']
        data.last_name = form.cleaned_data['last_name']
        data.street = form.cleaned_data['street']
        data.city = form.cleaned_data['city']
        data.save()
    else:
        return render_to_response('user/data_operations/error.html', {'errors': form._errors}, context_instance=RequestContext(request))

user_registered.connect(user_created)

My form :

class CustomRegistrationForm(RegistrationForm):
    first_name = forms.CharField(widget=forms.TextInput(attrs=attrs_dict), max_length=50)
    last_name = forms.CharField(widget=forms.TextInput(attrs=attrs_dict), max_length=50)
    street = forms.CharField(widget=forms.TextInput(attrs=attrs_dict), max_length=50)
    city = forms.CharField(widget=forms.TextInput(attrs=attrs_dict), max_length=50)

My model :

class UserProfile(models.Model):
    first_name = models.CharField(_("Name"), max_length=50, blank=False,)
    last_name = models.CharField(_("Last name"), max_length=50, blank=False,)
    street = models.CharField(_("Street"), max_length=50, blank=False,)
    city = models.CharField(_("City"), max_length=50, blank=False,) 
    user = models.ForeignKey(User, unique=True, related_name='profile',)

Registration form : class RegistrationForm(forms.Form):

email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
                                                           maxlength=75)),
                         label=_("Adres email"))
password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
                            label=_("Haslo"))
password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
                            label=_("Haslo powtorzone"))

def clean_email(self):
    email = self.cleaned_data.get("email")
    if email and User.objects.filter(email=email).count() > 0:
        raise forms.ValidationError(
            _(u"Already in use."))
    return email

回答1:


your 'user_registered' signal is sent after the User is saved. So it already has an 'email' field defined.

UPDATE


Using restless thinking :

form = CustomRegistrationForm(request.POST, request.FILES, notvalidateemail=True)

and in form :

def __init__(self, *args, **kwargs):
    self.notvalidateemail = kwargs.pop('notvalidateemail',False)
    super(CustomRegistrationForm, self).__init__(*args, **kwargs)

def clean_email(self):
    if self.notvalidateemail:
        return
    else:
        #your cleaning here
        return email



回答2:


Problem:

Your form is first saved by django-registration. Then you save it again in user_created.

Solutions:

  1. Use a different form in user_created. One that won't have already saved fields (these from User model like email). You just want to save additional data in user_created, right?

  2. Add some parameters to the form like:

in user_created:

form = CustomRegistrationForm(dontvalidateemail=True, request.POST, request.FILES)

and in form's init;

self.dontvalidateemail = dontvalidateemail

then just check it in clean_email.



来源:https://stackoverflow.com/questions/3182262/simple-form-not-validating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!