简体   繁体   English

登录后重定向错误

[英]Error redirecting after login

I am trying to redirect users to a create-profile form after signup.Since I don't know how to use dynamic url for LOGIN_REDIRECT_URL.I tried this little trick of linking it to a static view then finally to a dynamic view using redirect .It gives the error Reverse for 'add_profile' with arguments '()' and keyword arguments '{'username': u'badguy'}' not found. 0 pattern(s) tried: [] 我尝试在注册后将用户重定向到创建配置文件表单。由于我不知道如何为LOGIN_REDIRECT_URL使用动态网址。我尝试了将其链接到静态视图,然后最终使用redirect链接到动态视图的小技巧。 Reverse for 'add_profile' with arguments '()' and keyword arguments '{'username': u'badguy'}' not found. 0 pattern(s) tried: []它给出错误Reverse for 'add_profile' with arguments '()' and keyword arguments '{'username': u'badguy'}' not found. 0 pattern(s) tried: [] Reverse for 'add_profile' with arguments '()' and keyword arguments '{'username': u'badguy'}' not found. 0 pattern(s) tried: [] (with 'badguy' truly the username of the just signed-up user').From the error,it is clearly passing the username to the view.My codes are: Reverse for 'add_profile' with arguments '()' and keyword arguments '{'username': u'badguy'}' not found. 0 pattern(s) tried: [] (使用'badguy'确实是刚注册用户的用户名')。由于错误,它显然将用户名传递给了视图。我的代码是:

settings.py
LOGIN_REDIRECT_URL = '/prof-change/gdyu734648dgey83y37gyyeyu8g/'

urls.py
 url(r'^prof-change/gdyu734648dgey83y37gyyeyu8g/',views.login_redir,name='login_redir'),
 url(r'^create-profile/(?P<username>\w+)/$',views.add_profile,name='create-profile'),

views.py
def login_redir(request):
    user=get_object_or_404(User,username=request.user.username)
    username=user.username
    return redirect('add_profile',username=username)

def add_profile(request,username):
    userman=User.objects.get(username=username)
    ........
    ........

When you reverse urls (including when you use a url name with the redirect shortcut), you should use the name of the url pattern , not the view. 反向url时(包括当您使用带有redirect快捷方式的url名称时),应使用url模式的名称,而不是视图的名称。 The url pattern has name='create-profile' , so use that. 网址格式具有name='create-profile' ,因此使用它。

redirect('create-profile', username=username)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM