[英]How do you make a url that passes a parameter work in Django?
I'm working on registration logic and I can't seem to make the parameter pass in to work properly. 我正在研究注册逻辑,但似乎无法使参数传入以使其正常工作。 The error I get is a 404 page not found.
我得到的错误是找不到404页面。 Previously, I also got a “The view didn't return an HttpResponse object" error. Any help is appreciated.
以前,我还收到“视图未返回HttpResponse对象”错误。我们将提供任何帮助。
Here is my url from urls.py: 这是我来自urls.py的网址:
url(r'^accounts/confirm/(?P<activation_key>\d+)/$', 'mysite.views.confirm', name='confirm'),
This is my views.py: 这是我的views.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')
I send the url with a string that looks like: 我发送带有以下字符串的网址:
"Click the link to activate your account http://www.example.com/accounts/confirm/%s" % (obj.username, activation_key)"
So the link looks like this: http://www.example.com/accounts/confirm/4beo8d98fef1cd336a0f239jf4dc7fbe7bad8849a127d847f 因此,链接如下所示: http : //www.example.com/accounts/confirm/4beo8d98fef1cd336a0f239jf4dc7fbe7bad8849a127d847f
You have two issues here: 您在这里有两个问题:
/
from your pattern, or make it /?
/
或使其为/?
so it will be optional. /d+
will only match digits, and your link also contains other characters. /d+
仅匹配数字,并且您的链接还包含其他字符。 Try [a-z0-9]+
instead. [a-z0-9]+
。 Complete pattern: 完整模式:
^accounts/confirm/(?P<activation_key>[a-z0-9]+)$
Remove /
from end of your url: 从网址末尾删除
/
:
url(r'^accounts/confirm/(?P<activation_key>\d+)$', 'mysite.views.confirm', name='confirm'),
or add /
to end of your link: 或在链接末尾添加
/
:
http://www.example.com/accounts/confirm/4beo8d98fef1cd336a0f239jf4dc7fbe7bad8849a127d847f/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.