开发者

URL updating on login using django

开发者 https://www.devze.com 2023-01-07 11:23 出处:网络
I have started using django very recently . I am building a service with user login. Login , I am sending as POST service. There is a template to render , once the user is logged in.

I have started using django very recently . I am building a service with user login. Login , I am sending as POST service. There is a template to render , once the user is logged in. 开发者_JAVA技巧 But the browser address is not getting updated.

eg : abc.com the root my form would be something like

"form action="/login" .....>"

Once user press the login button browser URL wil be abc.com/login. After login I want the url to be "abc.com" again .

Pls help me .


If you use the built-in Django authentication, you can redirect after login or logout by using the built-in functions. You can redirect to a specific page or you can redirect them to the page they were trying to access.

For example, the doc above uses the following example in using the login_required decorator:

from django.contrib.auth.decorators import login_required

@login_required(redirect_field_name='redirect_to')
def my_view(request):
    ...

I highly recommend checking out the above doc. When I first setup Django's authentication, I was impressed that it only took me about 30 minutes to set it up the way I wanted it, and most of that time was reading the above page.


In you use login view, on successful login you should redirect to the page you want user to land on e.g.

def login_view(request):
    if not correct_login:
        pass#return response with errors
    else
        return HttpResponseRedirect("/welcome")

see HttpResponseRedirect or use redirect shortcut.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号