django的登录会话

时间:2014-09-03 19:49:42

标签: python django session

我正在尝试在我的网络应用中设置登录会话但无法让它工作。我是django的新手并阅读会话文档,但没有连接到我的网络应用程序。我现在想要的只是检查用户是否已登录,如果没有重定向到登录页面。

这是我尝试合并登录会话的代码。

settings.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'logins',
    'dashboards'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

urls.py

    from django.conf.urls import patterns, include, url
    from django.conf import settings
    from django.conf.urls.static import static
    from django.contrib import admin
    admin.autodiscover()

    urlpatterns = patterns('',
        url(r'^$', 'logins.views.login', name='login'),
        url(r'^accounts/auth/$', 'logins.views.auth_view', name='auth_view'),
        url(r'^accounts/dashboard/$', 'dashboards.views.dashboard', name='dashboard'),
        url(r'^accounts/logout/$', 'logins.views.logout', name='logout'),
        url(r'^accounts/invalid/$', 'logins.views.invalid', name='invalid'),

views.py for logins

    from django.shortcuts import render, render_to_response, RequestContext
    from django.http import HttpResponseRedirect, HttpResponse
    from django.contrib import auth
    from django.core.context_processors import csrf

    def login(request):
        c = {}
        c.update(csrf(request))
        return render_to_response('login.html', c)

    def auth_view(request):
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        user = auth.authenticate(username=username, password=password)

        if user is not None and user.is_active:
            auth.login(request, user)
            return HttpResponseRedirect('/accounts/dashboard')
        else:
            return HttpResponseRedirect('/accounts/invalid')

    def logout(request):
        auth.logout(request)
        return render_to_response('logout.html')

    def invalid(request):
        return render_to_response('invalid.html')

要登录的仪表板应用的views.py

from django.shortcuts import render, render_to_response, RequestContext
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib import auth
from django.core.context_processors import csrf

def dashboard(request):
    return render_to_response('dashboard.html')

2 个答案:

答案 0 :(得分:8)

在django中这很简单: 在此示例中,会话将保存在数据库中(您必须将django应用程序与数据库同步)

用户登录:

from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login
if request.method == 'POST':
    username = request.POST.get('nickname','')
    password = request.POST.get('password','')
    user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                request.session.set_expiry(86400) #sets the exp. value of the session 
                login(request, user) #the user is now logged in

对于其他网站(您需要登录的地方):

def my_func(request):
    if request.user.is_authenticated():
        print (user.id) #the user is loggedin

或者你使用login_require - 装饰者:

from django.contrib.auth.decorators import login_required

@login_required
def my_func(request):
    print(user.id) #the user is loggedin

答案 1 :(得分:4)

您可以使用Django的内置身份验证系统来检查用户是否已登录。您可以使用login_required装饰器。

views.py

from django.contrib.auth.decorators import login_required

@login_required
def my_view_login_required(request):
    return render_to_response('dashboard.html')