Mercurial > agora
comparison views.py @ 107:2bca07be6e51
Add login popup box
So you can log in and immediately be redirected to the same page. Sort
of like what reddit has. If Javascript is disabled, the user is simply
taken to the standard login page.
author | dellsystem <ilostwaldo@gmail.com> |
---|---|
date | Tue, 11 Sep 2012 20:23:51 -0400 |
parents | |
children | b8e0bdc37e32 |
comparison
equal
deleted
inserted
replaced
106:17bc502c65a4 | 107:2bca07be6e51 |
---|---|
1 from django.contrib.auth import login, authenticate | |
2 from django.contrib.auth.forms import AuthenticationForm | |
3 from django.shortcuts import render, redirect | |
4 from django.core.urlresolvers import reverse | |
5 from registration.forms import RegistrationForm | |
6 | |
7 from agora.apps.snippet.models import Snippet | |
8 | |
9 | |
10 def code(request): | |
11 context = { | |
12 'snippets': Snippet.objects.all()[:5], | |
13 'modules': None, # temp | |
14 'forge': None, # temp | |
15 } | |
16 | |
17 return render(request, 'code.djhtml', context) | |
18 | |
19 | |
20 def login_register(request): | |
21 form = None | |
22 next_url = None | |
23 | |
24 if request.method == 'POST': | |
25 action = request.POST.get('action') | |
26 next_url = request.GET.get('next') or reverse('login') | |
27 | |
28 if action == 'login': | |
29 username = request.POST.get('username', '') | |
30 password = request.POST.get('password1', '') | |
31 | |
32 if username and password: | |
33 user = authenticate(username=username, password=password) | |
34 login(request, user) | |
35 | |
36 return redirect(next_url) | |
37 else: | |
38 form = { | |
39 'password1': { | |
40 'errors': 'Please enter a username and password.', | |
41 }, | |
42 } | |
43 elif action == 'register': | |
44 form = RegistrationForm(request.POST) | |
45 | |
46 if form.is_valid(): | |
47 user = form.save() | |
48 login(request, user) | |
49 return redirect(next_url) | |
50 else: | |
51 # The action is not set. Malicious submission? | |
52 pass | |
53 | |
54 context = { | |
55 'next_url': next_url, | |
56 'form': form, | |
57 } | |
58 | |
59 return render(request, 'login.djhtml', context) |