diff options
Diffstat (limited to 'forums')
-rw-r--r-- | forums/forms.py | 6 | ||||
-rw-r--r-- | forums/settings.py | 1 | ||||
-rw-r--r-- | forums/urls.py | 4 | ||||
-rw-r--r-- | forums/views.py | 34 |
4 files changed, 45 insertions, 0 deletions
diff --git a/forums/forms.py b/forums/forms.py new file mode 100644 index 0000000..2e3a6d1 --- /dev/null +++ b/forums/forms.py @@ -0,0 +1,6 @@ +from django import forms + +class UserLoginForm(forms.Form): + username = forms.CharField() + password = forms.CharField(widget=forms.PasswordInput()) + diff --git a/forums/settings.py b/forums/settings.py index 3345be2..37d6202 100644 --- a/forums/settings.py +++ b/forums/settings.py @@ -143,6 +143,7 @@ INSTALLED_APPS = ( # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', 'website', + 'widget_tweaks', ) # A sample logging configuration. The only tangible logging diff --git a/forums/urls.py b/forums/urls.py index 1e4ee04..e129d4b 100644 --- a/forums/urls.py +++ b/forums/urls.py @@ -15,4 +15,8 @@ urlpatterns = patterns('', # Uncomment the next line to enable the admin: url(r'^admin/', include(admin.site.urls)), + + # User account urls + url(r'^accounts/login/', 'forums.views.user_login', name='user_login'), + url(r'^accounts/logout/', 'forums.views.user_logout', name='user_logout'), ) diff --git a/forums/views.py b/forums/views.py new file mode 100644 index 0000000..6890d61 --- /dev/null +++ b/forums/views.py @@ -0,0 +1,34 @@ +from django.http import HttpResponse, HttpResponseRedirect +from django.contrib.auth import login, logout, authenticate +from django.shortcuts import render_to_response +from django.core.context_processors import csrf + +from forums.forms import UserLoginForm + +def user_login(request): + if request.user.is_anonymous(): + if request.method == 'POST': + username = request.POST['username'] + password = request.POST['password'] + user = authenticate(username=username, password=password) + if user is not None: + if user.is_active: + login(request, user) + return HttpResponseRedirect('/') + else: + return HttpResponse('you are blocked') + else: + return HttpResponse('Invalid username or password') + else: + form = UserLoginForm() + context = { + 'form': form + } + context.update(csrf(request)) + return render_to_response('forums/templates/user-login.html', context) + else: + return HttpResponseRedirect('/') + +def user_logout(request): + logout(request) + return HttpResponseRedirect('/') |