django - how to login a user using forms? -


forms.py:

from django import forms django.contrib.auth import authenticate, get_user_model django.contrib.auth.models import user django.forms import modelform django.utils.text import capfirst .models import classname, sectionname, teachername, attendancename  class authenticationform(forms.form):     username = forms.charfield(max_length=20)     password = forms.charfield(widget=forms.passwordinput)     error_messages = {         'invalid_login': ("please enter correct %(username)s , password."                           "note both fields may case-sensitive."),         'inactive': ("this account inactive"),     }      def clean(self):         username = self.cleaned_data.get('username')         password = self.cleaned_data.get('password')          if username , password:             self.user_cache = authenticate(username=username, password=password)              if self.user_cache none:                 raise forms.validationerror(                     self.error_messages['invalid_login'],                     code='invalid_login',                 )             else:                 self.confirm_login_allowed(self.user_cache)          return self.cleaned_data      def confirm_login_allowed(self, user):         if user.is_active:             login(self.user)   /** raises error here... **/         else:             raise forms.validationerror(                 self.error_messages['inactive'],                 code='inactive',                 )      def get_user_id(self):         if self.user_cache:             return self.user_cache.id         return none      def get_user(self):         return self.user_cache 

views.py:

from django.shortcuts import render, get_object_or_404 django.core.urlresolvers import reverse django.contrib.auth import authenticate, login, logout django.contrib.auth.decorators import login_required django.http import http404, httpresponseredirect, httpresponse django.template.response import templateresponse django.views.generic import deleteview, listview django.views.decorators.csrf import csrf_protect django.views.decorators.cache import never_cache .models import classname, sectionname, teachername, attendancename .forms import classnameform, sectionnameform, teachernameform, attendancenameform, userform, passwordchangeform, authenticationform  def user_login(request):     if request.method == 'post':          form = authenticationform(request.post)          if form.is_valid():             return httpresponseredirect(reverse('student:mains'))         else:             print(form.errors)       else:         form = authenticationform()     return render(request, 'login.html', {'form': form},) 

urls.py:

from django.conf.urls import url, patterns django.contrib.auth import views auth_views . import views  urlpatterns = [     url(r'^register/$', views.register, name='register'),     url(r'^login/$', views.user_login, name='login'),     url(r'^logout/$', views.user_logout, name='logout'),     url(r'^password_change/$', auth_views.password_change, {'template_name': 'password_change_form.html', 'post_change_redirect': '/stu/password_change/done/'}, name="password_change"),     url(r'^password_change/done/$', auth_views.password_change_done, {'template_name': 'password_change_done.html'}, name="password_change_done"),     url(r'^restricted/', views.restricted, name='restricted'),     url(r'^mains/', views.mains, name = 'mains'), ] 

i'm new django authentication. i'm trying make things per docs have read. see above i've 'user_login' view , 'authenticationform' form.

i'm tring login user, don't know way how it? tried in view didn't worked.

then tried in 'can_login_allowed' form's method doesn't work.

is there other method implement it?

please! can me fix it?

thanks in advance....

the login function takes request first parameter. have write confirm_login_allowed this:

def confirm_login_allowed(self, request):         if self.user_cache.is_active:             login(request,self.user_cache)            else:             raise forms.validationerror(                 self.error_messages['inactive'],                 code='inactive',                 ) 

and call in view this

def user_login(request):     if request.method == 'post':          form = authenticationform(request.post)          if form.is_valid():              form.confirm_login_allowed(request)              return httpresponseredirect(reverse('student:mains'))         else:              .... 

also don't call confirm_login_allowed method clean method because does't have request.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -