错误:
TypeError at /confirmemail/amlqctnhel/
confirmemail() takes exactly 2 arguments (1 given)
Request Method: GET
Request URL: http://127.0.0.1:8000/confirmemail/amlqctnhel/
Django Version: 1.3.1
Exception Type: TypeError
Exception Value:
confirmemail() takes exactly 2 arguments (1 given)
Exception Location: /usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py in get_response, line 111
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path:
['/home/user1/djangoblog',
'/usr/lib/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib/python2.6/lib-old',
'/usr/lib/python2.6/lib-dynload',
'/usr/local/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages/gst-0.10',
'/usr/lib/pymodules/python2.6',
'/usr/lib/pymodules/python2.6/gtk-2.0']
url.py:
url(r'^confirmemail/[a-zA-Z0-9]{10}/$', 'blog.views.confirmemail'),
views.py:
def confirmemail(request,token):
user = Users.objects.get(email_token = token)
return render_to_response('confirmemail.html', {'user': user}, context_instance=RequestContext(request))
如果有人能帮助我修复它,我将不胜感激。谢谢。
最佳答案
在 URLconf 中,您需要在正则表达式中使用捕获组来实现 View 中的位置或关键字参数。如果您使用命名捕获组,则使用关键字参数;否则,使用位置参数。
您的 url()
行应如下所示:
url(r'^confirmemail/([a-zA-Z0-9]{10})/$', 'blog.views.confirmemail'),
# or
url(r'^confirmemail/(?P<token>[a-zA-Z0-9]{10})/$', 'blog.views.confirmemail'),
第一种形式使用位置参数(位置参数按 URL 中的捕获组排序)。第二种形式使用关键字参数,在本例中是token
。第二种形式有更多字符,但也可以安全地防止参数重新排序。
关于python - TypeError at/confirmemail/amlqctnhel/confirmemail() 恰好需要 2 个参数(给定 1 个),为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8929216/