我是django的新手。在这里,我想为我的django app创建登录。当我尝试创建用户帐户时,它会出现以下错误
FieldError at /signup/ Cannot resolve keyword 'mobile' into field
Choices are: date_joined, email, first_name, groups, id, is_active, is_staff, is_superuser, last_login, last_name, logentry, password, user_permissions, username, userregform
创建用户帐户时,我想添加用户名,电子邮件,手机号码,密码和组织名称。如果电子邮件或手机尚未存在,则应提供错误消息
这是我的代码
forms.py
User=get_user_model()
class UserLoginForm(forms.Form):
email=forms.EmailField(label='Email address')
password = forms.CharField(label='Password',
widget=forms.PasswordInput())
def clean(self,*args,**kwargs):
email=self.cleaned_data.get("email")
password=self.cleaned_data.get("password")
if email and password:
user=authenticate(email=email,password=password)
if not user:
raise forms.ValidationError("This email id is not registered")
if not user.check_password(password):
raise forms.ValidationError("Incorrect password")
return super(UserLoginForm,self).clean(*args,**kwargs)
class RegistrationForm(forms.ModelForm):
username= forms.CharField(label='Username')
email = forms.EmailField(label='Email address')
mobile =forms.CharField(label='Mobile No')
password = forms.CharField(label='Password')
org_name = forms.CharField(label='Organisation Name')
class Meta:
model = User
fields = ['username', 'email', 'mobile', 'password','org_name']
def clean_email(self):
email = self.cleaned_data.get('email')
check_duplicate_email = User.objects.filter (email=email).exists()
if check_duplicate_email:
raise forms.ValidationError(
"This email id is already registered"
)
return email
def clean_mobile(self):
mobile=self.cleaned_data.get('mobile')
check_duplicate_mobile=User.objects.filter(mobile=mobile).exists()
if check_duplicate_mobile:
raise forms.ValidationError(
"This mobile no is already registered"
)
return mobile
models.py
class UserRegForm(models.Model):
email=models.OneToOneField(User,unique=True)
mobile =models.CharField(max_length=10)
org_name = models.CharField(max_length=254)
views.py
def login(request):
title="Login"
form=UserLoginForm(request.POST or None)
if form.is_valid():
email=form.cleaned_data.get("email")
password = form.cleaned_data.get("password")
user=authenticate(email=email,password=password)
login(request,user)
return render(request,"userRegistration/login.html",{"form":form,"title":title})
def signup(request):
title="Signup"
if request.method=="POST":
form = RegistrationForm(request.POST)
print form.is_valid() #print False
if form.is_valid():
user=form.save(commit=False)
email = form.cleaned_data.get("email")
password = form.cleaned_data.get("password")
user=set_password(password)
user.save()
new_user = authenticate(email=user.email, password=password)
login(request, new_user)
return redirect("userRegistration/a.html")
else:
form=RegistrationForm()
return render(request, "userRegistration/signup.html", {"form":form,"title":title})
HTML
{% block title %}Registration{% endblock %}
{% block content %}
<h1>Registration</h1>
{% if form.errors %}
<h1>Error</h1>
{% endif %}
<form method="post" action="{% url 'userRegistration:signup' %}"">{% csrf_token %}
{{ form }}
<input type="submit" name="submit" />
</form>
{% endif %}
{% endblock %}</html>
答案 0 :(得分:0)
您的RegistrationForm
使用User
模型。我不知道它在哪里。 (也许django默认User
模型?)但是你不能使用其他模型中的字段。 (在这种情况下,UserRegForm
)。
如果您在注册时添加一些字段,则必须在注册完成后添加。
只需从表单中删除clean_mobile
,然后将其添加到signup
视图即可。如下所示(这只是一个例子。你必须按照自己的方式进行自定义
def signup(request):
title="Signup"
if request.method=="POST":
form = RegistrationForm(request.POST)
print form.is_valid() #print False
if form.is_valid():
user=form.save(commit=False)
email = form.cleaned_data.get("email")
password = form.cleaned_data.get("password")
user=set_password(password)
user.save()
# and save UserRegForm model here
# and call clean_mobile here
mobile = clean_mobile(form.cleaned_data.get('mobile')
UserRegForm.create(
email=user,
mobile=mobile
...
)
new_user = authenticate(email=user.email, password=password)
login(request, new_user)
return redirect("userRegistration/a.html")
else:
form=RegistrationForm()
return render(request, "userRegistration/signup.html", {"form":form,"title":title})