daily pastebin goal
36%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.contrib.auth import authenticate, login
  2. from django.core.urlresolvers import reverse
  3. from django import forms
  4. from django.http import HttpResponseRedirect, HttpResponse
  5. from django.shortcuts import render_to_response
  6. from django.template import RequestContext
  7.  
  8. def login(request):
  9.     if request.method == 'POST':
  10.         form = ContactForm(request.POST)
  11.         if form.is_valid():
  12.             login(request, form.user)
  13.             return HttpResponseRedirect ('/main/') # redirect after post
  14.     else:
  15.         form = ContactForm() # unbound form
  16.  
  17.     return render_to_response (
  18.         "login.html", {'form' : form, }, context_instance=RequestContext(request))
  19.  
  20. def main_page(request):
  21.     return HttpResponse("WOOHOO")
  22.  
  23. class ContactForm(forms.Form):
  24.     username = forms.CharField(max_length=100)
  25.     password = forms.CharField(widget=forms.PasswordInput(render_value=False))
  26.  
  27.     def clean(self):
  28.         user = self.user
  29.         if user and user.is_active:
  30.             return self.cleaned_data
  31.         raise forms.ValidationError("Username and password do not match")
  32.  
  33.     @property
  34.     def user(self):
  35.         return authenticate(
  36.             username=self.cleaned_data.get("username"),
  37.             password=self.cleaned_data.get("password"))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top