Django-fr

Forum

#1 16-09-2015 17:25:11

sympho
Membre
Inscription : 18-08-2015
Messages : 6

Formulaire

bonjour , nouveau avec Django j'ai un avec mon formulaire qui n'enregistre pas dans laBD:
voici mon code:

class RegisterForm(UserCreationForm):
    CHOICES = (('1', 'option1', ), ('2', 'option2'), ('3', 'option3'), ('4', 'option4'), ('5', 'option5'), )
    #user = User.objects.get()
    parrain = forms.CharField(widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': "le pseudo de votre parrain", 'readonly': "True"}))
    pays = forms.ChoiceField(widget=forms.Select(attrs={'class': "form-control"}), choices=CHOICES)
    username = forms.CharField(widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': "sympho"})  )
    password1 = forms.CharField(widget=forms.PasswordInput(attrs={'class': "form-control", 'placeholder': "Mot de Passe"}))
    password2 = forms.CharField(widget=forms.PasswordInput(attrs={'class': "form-control", 'placeholder': "password(again)"}))
    email = forms.EmailField(widget=forms.EmailInput(attrs={'class': "form-control", 'placeholder': "votre adresse mail"}))
    first_name = forms.CharField(widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': "Nom"}))
    last_name = forms.CharField(widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': "Prenom(s)"}))
    telephone = forms.CharField(widget=forms.TextInput(attrs={'class': "form-control"}))
    sponsor = forms.CharField(widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': "le pseudo de votre sponsor", 'readonly': "True", 'required': 'False'}))
    #lu_cgu = forms.CheckboxFieldRenderer
     #daccord = forms.CheckboxFieldRenderer
    class Meta:
        model = User
        fields = ["first_name", "last_name", "email", "username", "password1", "password2" ]
#         
#     # redefinis la fonction save pour inclure les champs ajoutés
    def clean_email(self):
        email = self.cleaned_data["email"]
        try:
            User._default_manager.get(email=email)
        except User.DoesNotExist:
            return email
        raise forms.ValidationError('cet email existe dejà')
        #pass
#     
    def clean_password2(self):
        # Check that the two password entries match
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Passwords don't match")
        return password2
#     
    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user_profile = UserProfil(user=user, pays=self.cleaned_data['pays'], telephone=self.cleaned_data['telephone'] )
       
        user.email = self.cleaned_data["email"]
        user.first_name = self.cleaned_data["first_name"]
        user.last_name = self.cleaned_data["last_name"]
        user.password2 = self.clean_password2["password2"]
       
        if commit:
            user.is_active = False
            new_user = user.save()
            new_user.save()
            user_profile.save()
            return new_user, user_profile


besoin d'aide s'il vous plait

Hors ligne

Pied de page des forums