Guest User

Untitled

a guest
Jun 14th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. from django.contrib.auth import authenticate, login
  2. from django import forms
  3. from django.http import HttpResponseRedirect, HttpResponse
  4. from django.shortcuts import render_to_response
  5. from django.template import RequestContext
  6.  
  7.  
  8. class ContactForm(forms.Form):
  9.     username = forms.CharField(max_length=100)
  10.     password = forms.CharField(widget=forms.PasswordInput(render_value=False))
  11.  
  12.     def clean(self):
  13.         if self.user is None:
  14.             raise forms.ValidationError("Username and password do not match")
  15.         return self.cleaned_data
  16.  
  17.     @property
  18.     def user(self):
  19.         user = authenticate(
  20.             username=self.cleaned_data.get("username"),
  21.             password=self.cleaned_data.get("password"))
  22.         if user and user.is_active:
  23.             return user
  24.  
  25.  
  26. def login(request):
  27.     if request.method == 'POST':
  28.         form = ContactForm(request.POST)
  29.         if form.is_valid():
  30.             login(request, form.user)
  31.             return HttpResponseRedirect ('/main/')
  32.     else:
  33.         form = ContactForm()
  34.     return render_to_response (
  35.         "login.html", {'form' : form}, context_instance=RequestContext(request))
  36.  
  37.  
  38. def main_page(request):
  39.     # dude, this shouldn't be in login.py, why bother creating separate files for your views
  40.     # if you're just going to poo them anywhere at random? ;-p
  41.     return HttpResponse("WOOHOO")
Add Comment
Please, Sign In to add comment