我的表单未提交我收到此错误请帮助!!! 我认为问题出在views.py.I我试图解决这个问题
File "/home/letsperf/mydjangoprojects/form/basic/views.py", line 9, in signupview
if user_form.is_valid():
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/forms/forms.py", line 183, in is_valid
return self.is_bound and not self.errors
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/forms/forms.py", line 175, in errors
self.full_clean()
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/forms/forms.py", line 384, in full_clean
self._clean_fields()
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/forms/forms.py", line 405, in _clean_fields
value = getattr(self, 'clean_%s' % name)()
File "/home/letsperf/mydjangoprojects/form/basic/forms.py", line 24, in clean_email
email_qs = UserProfile.object.filter(email=email)
AttributeError: type object 'UserProfile' has no attribute 'object'
[03 / Aug / 2017 09:24:52]“POST / signup / HTTP / 1.1”500 92520
views.py
from django.shortcuts import render
from basic.forms import UserProfileForm
# Create your views here.
def signupview(request):
if request.method == 'POST':
user_form = UserProfileForm(data=request.POST)
if user_form.is_valid():
password = user_form.cleaned_data.get('password')
confirm_password = user_form.cleaned_data.get('confirm_password')
user = UserProfileForm()
user.set_password(password)
user.set_password(confirm_password)
user.save()
return render(request, 'basic/register.html')
forms.py
class UserProfileForm(forms.ModelForm):
email = forms.EmailField(label="Email address")
password = forms.CharField(widget=forms.PasswordInput())
confirm_password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = UserProfile
fields = {'username', 'email', 'password', 'confirm_password'}
def clean_password(self, *args, **kwargs):
password = self.cleaned_data.get('password', None)
confirm_password = self.cleaned_data.get('confirm_password', None)
if password != confirm_password:
raise forms.ValidationError(" password didn't match ")
return super(UserProfile, self).clean_password(*args, **kwargs)
def clean_email(self):
email = self.cleaned_data.get('email')
email_qs = UserProfile.object.filter(email=email)
if email_qs.exist():
raise forms.ValidationError("This Email has already exists")
答案 0 :(得分:0)
这只是一个错字UserProfile.objects.filter(email=email)
。这是object
应该是复数 - objects
。
答案 1 :(得分:0)
试试这个:
email_qs = UserProfile.objects.filter(email=email)
或另一种方式:
email_qs = UserProfile.objects.get(email__iexact=email)
在这种情况下,如果没有匹配,您将获得DoesNotExist异常。