我正在研究注册逻辑,但似乎无法使参数传入正常工作。我收到的错误是找不到 404 页面。之前,我也遇到过“ View 没有返回 HttpResponse 对象”错误。非常感谢任何帮助。
这是我来自 urls.py 的网址:
url(r'^accounts/confirm/(?P<activation_key>\d+)/$', 'mysite.views.confirm', name='confirm'),
这是我的观点.py:
def confirm(request, activation_key):
if request.user.is_authenticated():
HttpResponseRedirect('/home')
user = Hash.objects.filter(hash_key = activation_key)
if user:
user = Hash.objects.get(hash_key = activation_key)
user_obj = User.objects.get(username= user.username)
user_obj.is_active = True
user_obj.save()
HttpResponseRedirect('/home')
我发送带有如下字符串的网址:
"Click the link to activate your account http://www.example.com/accounts/confirm/%s" % (obj.username, activation_key)"
所以链接看起来像这样: http://www.example.com/accounts/confirm/4beo8d98fef1cd336a0f239jf4dc7fbe7bad8849a127d847f
最佳答案
您这里有两个问题:
- 从模式中删除尾随的
/
,或将其设为/?
,这样它就是可选的。 /d+
只会匹配数字,您的链接还包含其他字符。请尝试使用[a-z0-9]+
。
完整模式:
^accounts/confirm/(?P<activation_key>[a-z0-9]+)$
关于python - 如何使 Django 中传递参数的 url 起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27257671/