Redirecting the django built-in logout view after

2019-03-29 11:30发布

问题:

I am using django to build a login and logout page , below are my codes

urls.py

from django.conf.urls.defaults import *
from django.conf import settings
from django.core.urlresolvers import reverse

urlpatterns = patterns('',
             url(r'^$', 'learn_django.views.home_page'),          
             url(r'^login/$', 'learn_django.views.login'),
             url(r'^logout/$', 'django.contrib.auth.views.logout', {'template_name': 'logout.html'}),
)

views.py

from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth import authenticate, login as main_login, logout as main_logout

def home_page(request):
    return render_to_response("home_page.html")

def login(request):
    .....
    .....
    return render_to_response("login.html")

logout.html

{% extends 'base.html' %}
{% block title %}Logout Page{% endblock %}
{% block body %}
  <div>
    <p style='color:#092E20;font-size:25px;font-weight:bold;padding-top:20px;padding-left:15px;'>
         You have been successfully logged out......
    </p>
    <p>Redirecting to login page.....</p> 
  </div>      
{% endblock %}

So in the above codes, i will have a login url which will display a login form and redirects to another url after successfull login which is working fine

Also i will have a logout url for which i am using the django built-in logout view , by supplying it a template logout.html which is also working fine and displaying the above html code successfully when i clicked the logout url

So now i want to redirect to the login page after displaying the logout page(after some time period....). I mean first the logout view should render the logout.html code and next should redirect to login page ....

Can anyone please let me know how to redirect to login html page after rendering logout.html as above ......

回答1:

Put this in your logout.html

<script>
    function redirect(){
       window.location.href = "supply_url_here";
    }

    setTimeout(redirect, 2000); //2000 is equivalent to 2 seconds
</script>


回答2:

You can use setTimeout() function to redirect to another page after specified amount of time.

{% block extrahead %}{{ block.super }}
    <script type="text/javascript">
        setTimeout(function() {
            window.location.href = "/login/";
        }, 2000);
    </script>
{% endblock %}

Add this after {% block title %}Logout Page{% endblock %}.



回答3:

as an alternative to javascript redirect, you can also do HTTP Refresh Header:

# in views.py
from django.contrib.auth import logout as main_logout

def logout(*args, **kwargs):
    resp = main_logout(*args, **kwargs)
    resp['Refresh'] = '3;URL=/account/login/' # redirects after 3 seconds to /account/login
    return resp

modify your urls.py as appropriate.

The advantage of this is that it will work even when javascript is disabled. The disadvantage is that it is only standard header by de facto, it's not specified in the HTTP standard.