Implementing the Environment:
1. System version:rh6.5
2. Python version:2.6.6
3. Django version:1.2.7
To create a project:
1, [[email protected] ~] #django-admin.py startproject MySite
2, [[email protected] mysite] #python manage.py startapp app01
3, [[email protected] MySite] #mkdir templates
4, [[email protected] mysite templates] #tourch login.html && tourch success.html
File configuration:
settings.py
# Django settings for MySite project.
DEBUG = True
Template_debug = DEBUG
ADMINS = (
# (' Your Name ', ' [email protected] '),
)
MANAGERS = ADMINS
DATABASES = {
' Default ': {
' ENGINE ': ' Django.db.backends.sqlite3 ', # Add ' postgresql_psycopg2 ', ' PostgreSQL ', ' MySQL ', ' sqlite3 ' or ' Oracle '.
' NAME ': ' Datebase_name ', # Or path to database file if using Sqlite3.
' USER ': ' Database_user ', # used with Sqlite3.
' PASSWORD ': ' Database_password ', # not used with Sqlite3.
' HOST ': ' Database_host ', # Set to empty string for localhost. Not used with Sqlite3.
' PORT ': ' Database_options ', # Set to empty string for default. Not used with Sqlite3.
}
}
# Local time zone for this installation. Choices can found here:
# Http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# Although not all choices is available on all operating systems.
# on Unix systems, a value of None would cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must BES set to the same as your
# System time zone.
Time_zone = ' America/chicago '
# Language code for this installation. All choices can is found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
Language_code = ' en-US '
site_id = 1
# If You set this to False, Django would make some optimizations so as not
# to load the internationalization machinery.
use_i18n = True
# If You set this to False, Django would not format dates, numbers and
# calendars According to the current locale
use_l10n = True
# Absolute filesystem path to the directory that'll hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/"
Media_root = ' '
# URL that handles the media served from Media_root. Make sure to use a
# trailing Slash If there is a path component (optional on other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
Media_url = ' '
# URL prefix for admin media--CSS, JavaScript and images. Make sure to use a
# trailing Slash.
# Examples: "http://foo.com/media/", "/media/".
Admin_media_prefix = '/media/'
# make this unique, and don ' t share it with anybody.
Secret_key = ' &4+fv=q&_#o86a$748*%yolle6&^3 (s1#5_k0!! A%q5swwq#uw '
# List of Callables that know what to import templates from various sources.
Template_loaders = (
' Django.template.loaders.filesystem.Loader ',
' Django.template.loaders.app_directories. Loader ',
# ' Django.template.loaders.eggs.Loader ',
)
Middleware_classes = (
' Django.middleware.common.CommonMiddleware ',
' Django.contrib.sessions.middleware.SessionMiddleware ',
# ' Django.middleware.csrf.CsrfViewMiddleware ',
' Django.contrib.auth.middleware.AuthenticationMiddleware ',
' Django.contrib.messages.middleware.MessageMiddleware ',
)
root_urlconf = ' Mysite.urls '
Template_dirs = (
"/root/mysite/templates"
# Put strings here, like "/home/html/django_templates" or "c:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don ' t forget to the use of absolute paths, not relative paths.
)
Installed_apps = (
' Django.contrib.admin ',
' Django.contrib.auth ',
' Django.contrib.contenttypes ',
' Django.contrib.sessions ',
' Django.contrib.sites ',
' Django.contrib.messages ',
' App01 '
# Uncomment the next line to enable the admin:
# Uncomment the next line to enable admin documentation:
# ' Django.contrib.admindocs ',
)
models.py
From django.db import Models
From Django.contrib Import admin
# Create your models here.
Class User (models. Model):
Username = models. Charfield (MAX_LENGTH=50)
Password = models. Charfield (MAX_LENGTH=50)
Admin.site.register (User)
[[email protected] mysite] #python manage.py syncdb//Sync Database
[Email protected]:~/djpy/mysite4$ python manage.py syncdb
Creating tables ...
Creating Table Django_admin_log
Creating Table Auth_permission
Creating Table Auth_group_permissions
Creating Table Auth_group
Creating Table Auth_user_groups
Creating Table Auth_user_user_permissions
Creating Table Auth_User
Creating Table Django_content_type
Creating Table Django_session
Creating Table Login_user
You just installed Django's auth system, which means you don ' t has any superusers defined.
Would to create one now? (yes/no): Yes input yes/no
Username (leave blank to use ' root '): Username (default current system user name)
Email address: [email protected] e-mail
Password: Password
Password (again): Confirm password
Superuser created successfully.
Installing Custom SQL ...
Installing indexes ...
Installed 0 object (s) from 0 fixture (s)
Access Admin
Log in user name and password the information that is set when we synchronize the database.
[email protected] mysite]# python manage.py runserver 0.0.0.0:5000
fill in the login account in this area http://localhost:5000/admin
urls.py
From django.conf.urls.defaults Import *
From django.shortcuts import Render_to_response
From django.shortcuts import HttpResponse
From Django.contrib Import admin
From APP01 Import views
# Uncomment the next lines to enable the admin:
# from Django.contrib Import admin
# Admin.autodiscover ()
Urlpatterns = Patterns ("',
# Example:
# (R ' ^mysite/', include (' Mysite.foo.urls ')),
# Uncomment the Admin/doc line below to enable admin documentation:
# (R ' ^admin/doc/', include (' Django.contrib.admindocs.urls ')),
# Uncomment the next line to enable the admin:
(R ' ^admin/', include (Admin.site.urls)),
(R ' ^login/', Views.login),
# (R ' ^$ ', Views.index),
)
views.py
#-*-codeing:uft8-*-
# Create your views here.
From django.shortcuts import Render_to_response
From django.shortcuts import HttpResponse
From django.http import Httpresponseredirect
From App01.models import User
From Django Import forms
Class UserForm (forms. Form):
Username = forms. Charfield (label= ' Username: ', max_length=100)
Password = forms. Charfield (label= ' Password: ', widget=forms. Passwordinput ())
DEF login (Request):
if Request.method = = ' POST ':
UF = UserForm (Request. POST)
If Uf.is_valid ():
Username = uf.cleaned_data[' username ']
Password = uf.cleaned_data[' password ']
user = User.objects.filter (username__exact=username,password__exact=password)
If User:
Return Render_to_response (' success.html ', {' username ': username})
Else
Return Httpresponseredirect ('/login/')
Else
UF = UserForm ()
Return Render_to_response (' login.html ', {' UF ': uf})
Login.html
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Login </title>
<style type= "Text/css" >
Body{color: #efd; background: #453;p adding:0 5em;margin:0}
H1{padding:2em 1em;background: #675}
H2{color: #bf8; border-top:1px dotted #fff; Margin-top:2em}
P{margin:1em 0}
</style>
<body>
<form method = ' Post ' enctype= "Multipart/form-data" >
{{uf.as_p}}
<input type= "Submit" value = "OK"/>
</form>
</body>
Success.html
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
</form>
</body>
To run the service:
[email protected] mysite]# python manage.py runserver 0.0.0.0:5000
In the Address bar, enter: Http://localhost:5000/login
This article is from the "Small Micro" blog, please make sure to keep this source http://guoshiwei.blog.51cto.com/2802413/1899959
Django Implements Web Login