python - TypeError at/confirmemail/amlqctnhel/confirmemail() 恰好需要 2 个参数(给定 1 个),为什么?

标签 python django django-views django-urls

错误:

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/

相关文章:

python - 如何使用 pip 和从启动板安装的 pypy?

python - 如何从命令行将十六进制值传递给 python 脚本

python - 如何获取传递给 Django REST API 的 ID?

django - 多个模型通用 ListView 到模板

django - 如何使用 django-userena 在注册时将用户分配给组?

python - 如何访问django模板中的字典值

python - 包含字符串和传统正则表达式的列表

python - 从任意多元函数中高效采样

django - 运行 n 次 django_content_type 查询

python - 是否可以在 ValidationError 的字符串中使用模板标签?