Login

Login + logout script from a nub

Author:
alandelevie
Posted:
December 21, 2008
Language:
Python
Version:
1.0
Tags:
forms login
Score:
-3 (after 3 ratings)

A simple way to log in and log out users. It's most likely nowhere near as good as the built-in login forms. This was more of a learning experience for me. Nevertheless, the script works. Usage: def some_view(request): check_login(request)

Some explanation can be found here: http://bit.ly/ete0

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#models.py:
from django import forms

class LoginForm(forms.Form):
	username = forms.CharField(max_length=100)
	password = forms.CharField(widget=forms.PasswordInput(render_value=False),max_length=100)

#views.py:
from project.courses.models import LoginForm
def check_login(request):
	def errorHandle(error):
		form = LoginForm()
		return render_to_response('pathto/login.html', {
				'error' : error,
				'form' : form,
		})
	if request.user.is_authenticated():
		username = request.user.username
		return render_to_response('pathto/logged_in.html', {
			'username': username,
		})
	else:
		if request.method == 'POST': # If the form has been submitted...
			form = LoginForm(request.POST) # A form bound to the POST data
			if form.is_valid(): # All validation rules pass
				username = request.POST['username']
				password = request.POST['password']
				user = authenticate(username=username, password=password)
				if user is not None:
					if user.is_active:
						# Redirect to a success page.
						login(request, user)
						return render_to_response('pathto/logged_in.html', {
							'username': username,
						})
					else:
						# Return a 'disabled account' error message
						error = u'account disabled'
						return errorHandle(error)
				else:
					 # Return an 'invalid login' error message.
					error = u'invalid login'
					return errorHandle(error)
			else: 
				error = u'form is invalid'
				return errorHandle(error)		
		else:
			form = LoginForm() # An unbound form
			return render_to_response('pathto/login.html', {
				'form': form,
			})

def logout_view(request):
	if request.user.is_authenticated():
		logout(request)
		form = LoginForm()
		return check_login(request)
	else: 
		check_login(request)
		return HttpResponseRedirect('/')


#login.html:
{% if error %}
<p><i>{{ error }}</i></p>
{% endif %}

<form action="." method="POST">
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>

#logged_in.html:
<h1>Welcome, {{ username }}</h1>
<p><a href='/scraper/logout/'>Logout</a></p>

More like this

  1. Set test cookie unless logged in by nipuL 6 years, 11 months ago
  2. Login via ExtJS by davidblewett 6 years, 8 months ago
  3. Password Obfuscation Log Filter by nikmolnar 1 year, 8 months ago
  4. LoginAsForm - Login as any User without a password by johnboxall 5 years, 9 months ago
  5. Decorator for authenticating token based API calls by jpulgarin 4 years, 1 month ago

Comments

Please login first before commenting.