3

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

それは私が見逃している小さなものでなければなりませんが、私には見えません。

4

1 に答える 1

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キャプチャされません。それが問題ですか?

また、問題を起こしている行の完全なトレースバックを提供してください。

さらに、 docによると、パスワードと電子メールを検証するロジックをclean()メソッドに入れる方がよいでしょう。

于 2012-10-28T05:46:17.457 に答える