2012-10-28 36 views
3

我想在Django中實現用戶註冊頁面。看起來像一個簡單的任務,但我得到一個我只是不明白的錯誤。這是我的觀點:沮喪Django用戶匹配查詢不存在消息

def registration_page(request): 
    if request.method == 'POST': 
    form = RegistrationForm(request.POST) 
    if form.is_valid(): 
     user = User.objects.create_user(
     username = form.cleaned_data['username'], 
     password = form.cleaned_data['password1'], 
     email = form.cleaned_data['email1'] 
    ) 
     return HttpResponseRedirect('/register/success/') 
    else: 
    form = RegistrationForm() 

    variables = RequestContext(request, { 
    'form': form 
    }) 
    return render_to_response('registration/registration_page.html', variables) 

這是我的登記表:

class RegistrationForm(forms.Form): 
    username = forms.CharField(label = u'Username', max_length = 30, error_messages={'required': 'A username is required.'}) 
    email1 = forms.EmailField(label = u'Email Address', error_messages={'required': 'An email address is required.'}) 
    email2 = forms.EmailField(label = u'Email Address confirmation', error_messages={'required': 'A confirmed email address is required.'}) 
    password1 = forms.CharField(label = u'Password', widget = forms.PasswordInput(), error_messages={'required': 'A password is required.'}) 
    password2 = forms.CharField(label = u'Password confirmation', widget = forms.PasswordInput(), error_messages={'required': 'A confirmed password is required.'}) 

    def clean_password2(self): 
    if 'password1' in self.cleaned_data: 
     password1 = self.cleaned_data['password1'] 
     password2 = self.cleaned_data['password2'] 
     if password1 == password2: 
     return password2 
    raise forms.ValidationError('Passwords must be identical. Remember, passwords are case-sensitive.') 

    def clean_email2(self): 
    if 'email1' in self.cleaned_data: 
     email1 = self.cleaned_data['email1'] 
     email2 = self.cleaned_data['email2'] 
     if email1 == email2: 
     if User.objects.get(email = email1): 
      raise forms.ValidationError("The email address '%s' is already associated with an account. This typically means you created an account in the past. Please use it." % email1) 
     else: 
      return email2 
    raise forms.ValidationError('Email addresses must be identical.') 


    def clean_username(self): 
    if 'username' in self.cleaned_data: 
     username = self.cleaned_data['username'] 
     if not re.search(r'^\w+$', username): 
     raise forms.ValidationError('Username can only contain letters, numbers, and the underscore characters.') 
     try: 
     User.objects.get(username = username) 
     except User.DoesNotExist: 
     return username 
    raise forms.ValidationError("Username '%s' is already taken." % username) 

的問題在form.is_valid彈出()和clean_username()。如果我添加一個不存在的用戶(用戶是否存在並不重要,行爲是一樣的),並用我知道有效的數據填充表單,那麼代碼應該簡單地從clean_username()返回用戶名,和form.is_valid()應該是True。相反,我得到一個錯誤頁面,看起來像這樣:

DoesNotExist at /register/ 
User matching query does not exist. 
Request Method: POST 
Request URL: http://127.0.0.1:8000/register/ 
Django Version: 1.4.1 
Exception Type: DoesNotExist 
Exception Value:  
User matching query does not exist. 
Exception Location: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py in get, line 366 
Python Executable: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python 
Python Version: 2.7.2 
Python Path:  
['/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg', 
'/Library/Python/2.7/site-packages/bpython-0.10.1-py2.7.egg', 
'/Library/Python/2.7/site-packages/Pygments-1.5-py2.7.egg', 
'/Library/Python/2.7/site-packages/django_registration-0.8-py2.7.egg', 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL', 
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info', 
'/Library/Python/2.7/site-packages'] 
Server time: Sun, 28 Oct 2012 00:51:58 -0400 

這一定是小東西,我失蹤,但我無法看到它。

+0

查看代碼,'db/models/query.py'中的第366行非常明確。如有疑問,請簡單:創建一個5(或10)行CLI腳本並嘗試獲取該用戶名。怎麼了?嘗試在'phpMyAdmin'(或您選擇的數據庫瀏覽器)中查看數據庫。你看到了什麼? –

回答

5

在你的代碼

def clean_email2(self): 
    ... 
     # here 
     if User.objects.get(email = email1): 
      raise forms.ValidationError("The email address '%s' is already associated with an account. This typically means you created an account in the past. Please use it." % email1) 
    ... 

get()可能導致DoesNotExist但沒有捕獲。這是問題嗎?

此外,請提供完整追溯線條使問題。

此外,根據the doc,它更好地把驗證密碼的邏輯&電子郵件的clean()方法。

+0

okm,非常感謝! get()拋出未捕獲的異常是問題所在。我知道這會是我錯過的小事。總是有幫助有第二套眼睛看我的代碼。 – nucklehedd