Building a login module in Django -


i'm trying build login module in django works independently page on.

i'm defining login module in base.html template contains header (the login module sits in header) other pages extend base template.

my plan pass error through context dictionary, problem don't know how make function render() render template user attempted login.

this view code:

def login(request):     context_dict = {}     if request.post:         username = request.post.get('login_username')         password = request.post.get('login_password')          user = authenticate(username=username, password=password)          if not user:             context_dict['error'] = 'שם משתמש או סיסמא אין נכונים'         elif not user.is_active:             context_dict['error'] = 'חשבונך נחסם, אם הינך חושב שזאת טעות צור קשר עם מנהל בהקדם.'         else:             login(request,user)             redirect(request.meta.get('http_referer'))      render(request, ', context_dict) 

finally figured out, trick using django messages framework.

here code , explanation wants implement similar:

simple url routing in urls.py file

url(r'^login/$', views.login, name='login'), url(r'^logout/$', views.logout, name='logout'), 

two views, 1 login 1 logout

def login(request):     if request.post:         username = request.post.get('login_username')         password = request.post.get('login_password')          user = authenticate(username=username, password=password)          if not user:             messages.add_message(request, messages.info, 'wrong username or password')          elif not user.is_active:             messages.add_message(request, messages.info, 'your account disabled')          else:             django_login(request,user)             return redirect(request.meta.get('http_referer'))      return redirect(request.meta.get('http_referer'))   def logout(request):     django_logout(request)     return redirect(request.meta.get('http_referer')) 

modified import avoid recursion error (in views.py)

from django.contrib.auth import authenticate, login django_login, logout django_logout 

and displaying message in template

{% if messages %}     {% message in messages %}         <div class='login_error'>             {{ message }}         </div>     {% endfor %} {% endif %} 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -