Login sessions for django

I am trying to establish login sessions in my web application but cannot make it work. I am new to django and reading the documentation for the sessions, but not connecting to my web application. All I want now is to check if the user is registered and is not being redirected to the login page.

Here is the code I'm trying to include in login sessions.

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 to login

  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 for a toolbar application that registers with

 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') 
+5
source share
2 answers

It is very simple to do it in django: In this example, the sessions will be saved in db (you need to synchronize the django application with your database).

User Login:

 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 

And for other sites (where you need to register):

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

Or you use login_require -decorator:

 from django.contrib.auth.decorators import login_required @login_require def my_func(request): print(user.id) #the user is loggedin 
+6
source

You can use Django's built-in authentication system, which checks if the user is logged in or not. You can use the login_required decorator.

views.py

 from django.contrib.auth.decorators import login_required @login_required def my_view_login_required(request): return render_to_response('dashboard.html') 
+4
source

Source: https://habr.com/ru/post/1201764/


All Articles