![](/img/trans.png)
[英]django “type object 'UserProfile' has no attribute 'DoesNotExist' ”
[英]type object 'UserProfile' has no attribute 'object'
我的表單未提交我收到此錯誤,請幫助!!! 我認為問題出在views.py中,我試圖解決一段時間
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')
表格
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")
這只是一個錯字UserProfile.objects.filter(email=email)
。 這個object
應該是復數objects
。
試試這個:
email_qs = UserProfile.objects.filter(email=email)
或另一種方式:
email_qs = UserProfile.objects.get(email__iexact=email)
在這種情況下,如果沒有匹配項,您將獲得DidNotExist Exception。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.