简体   繁体   中英

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: [] 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:

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. The url pattern has name='create-profile' , so use that.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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