Ad

How Redirect Login.html To Chat.html Page After Successful Logged In Django Framework?

- 1 answer

I have used template/login.html for logging in, after successful logged in how to redirect the login.html page to chatwindow.html page (some other html) in Django framework.

So, do I need to redirect it from views.py file or from login.html?

Any help highly appreciated.

Ad

Answer

Django implements multiple ways to do this:

1. LOGIN_REDIRECT_URL

This is a global setting in your settings.py. It works for all login pages when next= parameter is not specified in login url (e.g. example.com/login?next=/foo).

2. Use next parameter in the login URL

This is usually used to customize the login redirect for individual cases.

The common use-case is when you use @login_required decorator. When a user tries to access a page which requires authentication, a user is then redirected to a login page with a next= parameter pointing to the current page. For example if the user went to /secure/page, then the login page will be something like /login?next=/secure/page. After the user will successfully authenticate, Django will redirect them back to the protected page.

3. Use the hidden input next on the login page

Finally you can set the redirect path in the login form itself:

<form method="POST" ...>
    <input typy="hidden" name="next" value="/secure/page">
    ...
</form>

I would guess that first method might be the most appropriate in your case however keep in mind the other options if you will need them.

Ad
source: stackoverflow.com
Ad