Hi,
I am new to django, and I am trying to create a custom login
application, from where the users might get access to other
applications in the system. I can not get it to work, please, I need
help.
Project structure:
txm--+--profile
+--app1
+-- ap2
txm.urls
###############################################################################
# IMPORTS
###############################################################################
from django.conf.urls.defaults import *
# Imports from self
from txm.views import Home
from txm.profile.views import Login #, Logout, Change, Lost
#from txm.docreg.views import Docreg
#from txm.hlrsd.views import Hlrsd
###############################################################################
# BINDINGS
###############################################################################
urlpatterns = patterns('',
# Uncomment this for admin:
(r'^admin/$', include('django.contrib.admin.urls')),
# TXM User Profile
(r'^$', Home),
(r'^login/$', Login),
#(r'^logout/$', Logout),
#(r'^change/$', Change),
#(r'^lost/$', Lost),
# TXM DocReg
#(r'^docreg/$', Docreg),
# TXM HLR Subscriber Data
#(r'^hlrsd/$', Hlrsd),
)
txm.views
###############################################################################
# IMPORTS
###############################################################################
# Imports from Python
# Imports from Django
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
# Imports from self
###############################################################################
# CONSTANTS
###############################################################################
###############################################################################
# VIEWS
###############################################################################
def Home(request):
"""System home/main page
This is the main page, from where the system provided
applications
can be delivered.
"""
if request.user.is_authenticated():
return render_to_response('applications.html', {})
return HttpResponseRedirect('/login')
tmx.profile.views
###############################################################################
# IMPORTS
###############################################################################
# Imports from Python
# Imports from Django
from django.shortcuts import render_to_response
from django import newforms as forms
from django.core import validators
# Imports from self
from txm.profile.forms import LoginForm
###############################################################################
# CONSTANTS
###############################################################################
###############################################################################
# VIEWS
###############################################################################
def Login(request):
""" User login
User login to system access
"""
form = new_data = message = None
# Verify correct method
if request.GET:
form = LoginForm()
new_data = request.GET.copy()
message = new_data['message']
return render_to_response('login.html', {'form':form,
'message':message})
if request.POST:
# Proceed to acquire user login
form = LoginForm(request.POST)
new_data = request.POST.copy()
message = new_data['message']
# Validate and authenticate user login
if form.is_valid():
return render_to_response('applications.html', {})
else:
return render_to_response('login.html', {'form':form,
'message':message})
return render_to_response('login.html', {'form':form,
'message':message})
txm.profile.forms
###############################################################################
# IMPORTS
###############################################################################
# Imports from Python
import re
# Imports from Django
from django import newforms as forms
from django.core import validators
from django.contrib.auth import authenticate, login
###############################################################################
# CONSTANTS
###############################################################################
# Use custom form validator for:
# - User name
# - User password
# User name
user_name_length_min = 3
user_name_length_max = 15
user_name_regex = r'^[a-zA-Z0-9\.\_\-]{%d,%d}$' %
(user_name_length_min, user_name_length_max)
user_name_msg = u'User name validation must be %s-%s long, can
only contain letters, numbers, underscores, dash, and dot'
# User password
user_password_length_min = 6
user_password_length_max = 15
user_password_regex = r'^[a-zA-Z0-9\.\_\-]{%d,%d}$' %
(user_password_length_min, user_password_length_max)
user_password_msg = u'User password validation must be %s-%s
long, can only contain letters, numbers, underscores, dash, and dot'
# Do not touch!!!
user_name_re = re.compile(user_name_regex)
user_password_re = re.compile(user_password_regex)
###############################################################################
# FORMS
###############################################################################
class LoginForm(forms.Form):
"""User login form
1) Build custom form layout
2) Custom validation of user and password correctness
3) Authenticate against django.contrib.auth
"""
# Form declaration
#username = forms.CharField(label = u'username',
# widget = forms.TextInput(),
# min_length = user_name_length_min,
# max_length = user_name_length_max,
# required = True,
# help_text = user_name_msg),
#password = forms.CharField(label = u'password',
# widget =
forms.PasswordInput(render_value=False),
# min_length = user_password_length_min,
# max_length = user_password_length_max,
# required = True,
# help_text = user_password_msg)
username = forms.CharField(label = u'username',
widget = forms.TextInput(),
max_length = user_name_length_max)
password = forms.CharField(label = u'password',
widget = forms.PasswordInput(),
max_length = user_password_length_max)
# Initialize
def __init__(self, request):
self.request = request
# Custom validation of user name
# According to the rules setup in the constants section
def clean_username(self):
if not user_name_re.search(self.cleaned_data[u'username']):
raise forms.ValidationError(user_name_msg)
return self.cleaned_data[u'username']
# Custom validation of user password
# Accordng to the rules setup in the constantst section
def clean_password(self):
if not
user_password_re.search(self.cleaned_data[u'password']):
raise forms.ValidationError(user_password_msg)
return self.cleaned_data[u'password']
# User validation, authentication, and login
def clean(self):
self.username = self.clean_username()
self.password = self.clean_password()
self.user = authenticate(username=self.username,
password=self.password)
if self.user is not None:
if self.user.is_active:
login(self.request, self.user)
else:
raise validators.ValidationError, u'The account is
locked'
else:
raise validators.ValidationError, u'User does not exist'
txm.templates.profile.login.html
{% extends "base.html" %}
{% block content %}
<div id="content">
<h2>Login</h2>
<form method="post" action="/login/">
{% if message %}
<p>{{ message }}</p>
{% endif %}
<p><label>Username:</label><span class="error">{{ errors }}</span><br/
>{{ form.username }}</p>
<p><label>Password:</label><span class="error">{{ errors }}</span><br/
>{{ form.password }}</p>
<p><input type="submit" value="Login"></p>
<p><a href="/lost/">Lost Password</a></p>
</form>
</div>
{% endblock %}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---