我正在尝试实现丢失的密码时的ValueError

时间:2011-10-05 08:07:30

标签: python django

我正在使用Python,我是Python语言的新手,所以我正在尝试重置丢失的密码,但我遇到了ValueError Exception,这是我的代码:

models.py:

def forgot(request):
    if request.method == 'POST':
        try:
            user = User.objects.get(username=request.POST['username'])
            lostpassword = LostPassword.objects.create(user=user, key=new_key())
            message = 'http://%s:%s/lostpassword/change/%s/' % (
                request.META['SEVER_NAME'],
                request.META['SEVER_PORT'],
                lostpassword.key)
        except User.DoesNotExist:
            message = 'Unknown user'
    else:
        message = ''
    return render_to_response('homepage/forgot.html', {'message': message})

forms.py:

  class ForgotForm(forms.Form):
      help_string="Your password will be emailed to you"
      user = forms.CharField()

views.py:

  def forgot(request):
      if request.method == 'POST':
          try:
              user = User.objects.get(username=request.POST['username'])
              lostpassword = LostPassword.objects.create(user=user, key=new_key())
              message = 'http://%s:%s/lostpassword/change/%s/' % (
                  request.META['SEVER_NAME'],
                  request.META['SEVER_PORT'],
                  lostpassword.key)
          except User.DoesNotExist:
              message = 'Unknown user'
      else:
          message = ''
      return render_to_response('homepage/forgot.html', {'message': message})

url.py:

     urlpatterns = patterns('blog.apps.homepage.views',
 url(r'^$', 'index', name='homepage_index'),
 url(r'^about/$', 'about', name='homepage_about'),
 url(r'^contact/$', 'contact', name='homepage_contact'),
 url(r'^archive/$', 'archive', name='homepage_archive'),
 url(r'^login/$', 'login', name='homepage_login'),
 url(r'^register/$', 'register', name='homepage_register'),
 url(r'^gallery/$', 'gallery', name='homepage_gallery'),
 url(r'^profile/$', 'profile', name='homepage_profile'),
 url(r'^welcome/$', 'welcome', name='homepage_welcome'),
 url(r'^validation/$', 'validation', name='homepage_welcome'),
 url(r'^success/$', 'success', name='homepage_success'),
 url(r'^add_organisation/$', 'add_organisation', name='homepage_add_organisation'),
 url(r'^hallo/$', 'hallo', name='homepage_hallo'),
 url(r'^editMe/$', 'editMe', name='homepage_editMe'),
     url(r'^forgot/$', 'forgot', 'blog.apps.homepage.views.forgot'),

)

forgot.html(模板):

<form method="post" action=".">
    Username: <input type"text" name="username" /> <br />
    <input type="Submit">
</form>
<p>
    {{ message }}
</p>

回溯:

Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
101. request.path_info)
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in resolve
252. sub_match = pattern.resolve(new_path)
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in resolve
252. sub_match = pattern.resolve(new_path)
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in resolve
156. kwargs.update(self.default_args)

Exception Type: ValueError at /forgot/ 
Exception Value: dictionary update sequence element #0 has length 1; 2 is required

我不做的是什么?

1 个答案:

答案 0 :(得分:0)

url函数的第三个参数应该是一个由传递给视图函数的额外参数组成的字典。出于某种原因,您已经有了'blog.apps.homepage.views.forgot' - 但您已经设置了视图函数forgot

顺便说一句,请注意Django已经包含“重置密码”功能 - 请参阅the docs