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
[email protected] mysite]# cat 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 ', # Add ' postgresql_psycopg2 ', ' PostgreSQL ', ' MySQL ', ' sqlite3 ' or ' Oracle '.
' NAME ': ', # Or path to database file if using Sqlite3.
' USER ': ', # not used with Sqlite3.
' PASSWORD ': ', # used with Sqlite3.
' HOST ': ', # Set to empty string for localhost. Not used with Sqlite3.
' PORT ': ', # 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 = ' 2+^[email protected][email protected]=&u=+cl520dij-!w8=t7eh-(0b (^8hb#w ')
# 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.middleware.csrf.CsrfResponseMiddleware ',
' 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.auth ',
' Django.contrib.contenttypes ',
' Django.contrib.sessions ',
' Django.contrib.sites ',
' Django.contrib.messages ',
' App01 '
# Uncomment the next line to enable the admin:
# ' Django.contrib.admin ',
# Uncomment the next line to enable admin documentation:
# ' Django.contrib.admindocs ',
)
[Email protected] mysite]#
models.py
urls.py
[email protected] mysite]# cat urls.py
From django.conf.urls.defaults Import *
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 ' ^$ ', Views.index),
(R ' ^login/$ ', Views.login),
)
[Email protected] mysite]#
views.py
[email protected] app01]# cat views.py
# Create your views here.
#-*-Coding:utf8-*-
From django.shortcuts import Render_to_response
From django.http import HttpResponse
From django.template import RequestContext
def index (Request):
Return Render_to_response (' index.html ')
DEF login (Request):
Count = 0
User_list = {' GSW1 ': ' 111 ', ' gsw2 ': ' 222 ', ' gsw3 ': ' 333 '}
Username = Request. get[' username ']
Password = Request. get[' Password ']
If User_list.has_key (username):
Pass
Else
Alert = Username + ' is not exist. '
Return Render_to_response (' index.html ', {' Alert ': alert})
If username in Lock:
Alert = "Your account was locked,please contact administrator."
Return Render_to_response (' index.html ', {' Alert ': alert})
Else
While Count < 3:
If user_list[username] = = Password:
#alert = ' Welcome ' + username
Return Render_to_response (' success.html ')
Else
Alert = ' Password Error '
Count + = 1
Return Render_to_response (' index.html ', {' Alert ': alert})
Else
Lock.append (username)
Return Render_to_response (' index.html '})
[Email protected] app01]#
Index.html
[email protected] templates]# cat index.html
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>Title</title>
<body>
<a href= "http://127.0.0.1:8000" >index</a>
<form method= "Get" action= "/login/" >
Username:<input type= "text" name= "Username" ><br>
Password:<input type= "Password" name= "Password" ><br>
<input type= "Submit" value= "Login" >
</form>
{{Alert}}
{{count}}
</body>
[Email protected] templates]#
Success.html
[email protected] templates]# cat success.html
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>Title</title>
<body>
<a href= ' http://127.0.0.1:8000 ' >index</a>
Welcome
</body>
[Email protected] templates]#
This article is from the "Small Micro" blog, please make sure to keep this source http://guoshiwei.blog.51cto.com/2802413/1900933
Django implements Web Landing (second edition)