python - Registering with Django not working -
i have create website project , encountered problem registering simple user django : seems data register form not saved in database.
it nice if knew did wrong (i beginner django) because can't seem find problem from.
here files :
models.py :
from django.db import models # create models here. class utilisateur(models.model): pseudo = models.charfield(primary_key=true, max_length=100) password = models.charfield(max_length=50) nom = models.charfield(max_length=200) prenom = models.charfield(max_length=200) account_choices = ( ("u", "utilisateur"), ("h", "historien"), ("f", "famille"), ) compte = models.charfield( max_length=1, choices=account_choices, default="u" ) datenaissance = models.datefield(null=true, blank=true) sexe_choices = ( ("m", "masculin"), ("f", "féminin"), ) sexe = models.charfield( max_length=1, choices=sexe_choices, default="?" ) email = models.emailfield(max_length=254)
views.py :
from django.shortcuts import (render, render_to_response, redirect) .forms import utilisateurinscriptionform django.core.context_processors import csrf django.contrib.auth import (authenticate, login) def inscription_view(request): title = "inscription" form = utilisateurinscriptionform(request.post or none) if form.is_valid(): new_user = form.save() new_user = utilisateur( pseudo = form.cleaned_data['pseudo'], password = form.cleaned_data['password'], compte = form.cleaned_data['compte'], email = form.cleaned_data['email'] ) new_user.set_password(password) #we save user in database new_user.save() #we check person registered new_user = authenticate(username=pseudo, password=password) if new_user not none: if new_user.is_active: login(request, new_user) return redirect("/") context = { "form": form, "title": title } return render(request, "inscription.html", context)
forms.py :
from django import forms .models import utilisateur class utilisateurinscriptionform(forms.modelform): password1 = forms.charfield(widget=forms.passwordinput(attrs=dict(required=true, max_length=30, render_value=false)), label='mot de passe') email2 = forms.emailfield(label='email (confirmation)') password2 = forms.charfield(widget=forms.passwordinput(attrs=dict(required=true, max_length=30, render_value=false)), label='mot de passe (confirmation)') class meta: model = utilisateur fields = [ 'pseudo', 'password1', 'password2', 'email', 'email2', 'compte', ] def clean_email(self): print(self.cleaned_data) email = self.cleaned_data.get('email') email2 = self.cleaned_data.get('email2') if email != email2: raise forms.validationerror("email must match !") return email
i searched long time in order find wrong found nothing useful. in advance !
Comments
Post a Comment