Python 向Django注册不起作用
我必须为一个项目创建一个网站,我在向Django注册一个简单的用户时遇到了一个问题:似乎注册表中的数据没有保存在数据库中 如果有人知道我做错了什么就好了(我是Django的初学者),因为我似乎无法找到问题的根源 这是我的档案: 型号.py:Python 向Django注册不起作用,python,django,Python,Django,我必须为一个项目创建一个网站,我在向Django注册一个简单的用户时遇到了一个问题:似乎注册表中的数据没有保存在数据库中 如果有人知道我做错了什么就好了(我是Django的初学者),因为我似乎无法找到问题的根源 这是我的档案: 型号.py: from django.db import models # Create your models here. class Utilisateur(models.Model): pseudo = models.CharField(primary_k
from django.db import models
# Create your 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)
from django.shortcuts import (render, render_to_response, redirect)
from .forms import UtilisateurInscriptionForm
from django.core.context_processors import csrf
from 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 the user in the database
new_user.save()
#We check is the person is registered
new_user = authenticate(username=pseudo, password=password)
if new_user is not None:
if new_user.is_active:
login(request, new_user)
return redirect("/")
context = {
"form": form,
"title": title
}
return render(request, "inscription.html", context)
from django import forms
from .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
视图.py:
from django.db import models
# Create your 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)
from django.shortcuts import (render, render_to_response, redirect)
from .forms import UtilisateurInscriptionForm
from django.core.context_processors import csrf
from 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 the user in the database
new_user.save()
#We check is the person is registered
new_user = authenticate(username=pseudo, password=password)
if new_user is not None:
if new_user.is_active:
login(request, new_user)
return redirect("/")
context = {
"form": form,
"title": title
}
return render(request, "inscription.html", context)
from django import forms
from .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
forms.py:
from django.db import models
# Create your 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)
from django.shortcuts import (render, render_to_response, redirect)
from .forms import UtilisateurInscriptionForm
from django.core.context_processors import csrf
from 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 the user in the database
new_user.save()
#We check is the person is registered
new_user = authenticate(username=pseudo, password=password)
if new_user is not None:
if new_user.is_active:
login(request, new_user)
return redirect("/")
context = {
"form": form,
"title": title
}
return render(request, "inscription.html", context)
from django import forms
from .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
我搜索了很长一段时间,想找出什么地方可能出错,但我没有发现任何有用的东西。
提前谢谢
form.save()
将为您保存模型。你为什么要再次尝试保存它?此外,您应该会收到一个IntegrityError
,因为您的模型(nom
,prenom
)中有不可为空的字段,这些字段不是表单提供的。我想我需要指定表单数据和模型参数之间的链接。我想我错了。此外,我没有在控制台上出现“IntegrityError”,这可能是功能失调背后的原因吗?