2017-08-03 64 views
2

我的表單沒有提交我得到這個錯誤請幫助!!! 我認爲問題出在views.py.I正在試圖解決這個一會兒type object'UserProfile'沒有屬性'object'

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/8/2017年9時24分52秒] 「POST /註冊/ 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

它只是一個錯字UserProfile.objects.filter(email=email)。這是object應該是複數 - objects。在這種情況下

email_qs = UserProfile.objects.get(email__iexact=email) 

如果沒有匹配的,你會得到DoesNotExist例外:

0

試試這個:

email_qs = UserProfile.objects.filter(email=email) 

或者另一種方式。

相關問題