问题
Error:
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']
urls.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))
It will be highly appreciated if someone can help me to fix it. Thank You.
回答1:
In a URLconf, you need to use capturing groups in your regex to achieve positional or keyword arguments in your view. If you use a named capture group, then keyword arguments are used; otherwise, positional arguments are used.
Here is what your url()
line should look like:
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'),
The first form uses a positional argument (and positional arguments are ordered by the capture groups in the URL). The second form uses a keyword argument, in this case token
. The second form is more characters but will also be safe against parameter reordering.
回答2:
You arent capturing a pattern in your url so its not passing a value for your token parameter
url(r'^confirmemail/([a-zA-Z0-9]{10})/$', 'blog.views.confirmemail'),
Note i have wrapped your pattern in a capture group
来源:https://stackoverflow.com/questions/8929216/typeerror-at-confirmemail-amlqctnhel-confirmemail-takes-exactly-2-arguments