Django 实现WEB登陆(第二版)

简介:

实现环境:

1、System version:rh6.5

2、Python version:2.6.6

3、Django version:1.2.7

创建项目:

1、[root@localhost ~]#django-admin.py startproject mysite

2、[root@localhost mysite]#python manage.py startapp app01

3、[root@localhost mysite]#mkdir templates 

4、[root@localhost mysite templates]#tourch login.html && tourch success.html

文件配置:

settings.py

[root@localhost mysite]# cat settings.py

# Django settings for mysite project.


DEBUG = True

TEMPLATE_DEBUG = DEBUG


ADMINS = (

    # ('Your Name', 'your_email@domain.com'),

)


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': '',                  # Not 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 be found here:

# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name

# although not all choices may be available on all operating systems.

# On Unix systems, a value of None will cause Django to use the same

# timezone as the operating system.

# If running in a Windows environment this must be set to the same as your

# system time zone.

TIME_ZONE = 'America/Chicago'


# Language code for this installation. All choices can be found here:

# http://www.i18nguy.com/unicode/language-identifiers.html

LANGUAGE_CODE = 'en-us'


SITE_ID = 1


# If you set this to False, Django will make some optimizations so as not

# to load the internationalization machinery.

USE_I18N = True


# If you set this to False, Django will not format dates, numbers and

# calendars according to the current locale

USE_L10N = True


# Absolute filesystem path to the directory that will 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 in 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+^0fao!r+lv@z=u@=&u=+cl520dij-!w8=t7eh-(0b(^8hb#w'


# List of callables that know how 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 use 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',

)

[root@localhost mysite]#

models.py


urls.py


[root@localhost mysite]# cat urls.py

from django.conf.urls.defaults import *

from app01 import views


# Uncomment the next two 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),

)

[root@localhost mysite]#



views.py


[root@localhost 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 is 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'})

[root@localhost app01]#



index.html

[root@localhost templates]# cat index.html 

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Title</title>

</head>

<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>

</html>

[root@localhost templates]#



success.html

[root@localhost templates]# cat success.html 

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Title</title>

</head>

<body>

<a href='http://127.0.0.1:8000'>index</a>

Welcome

</body>

</html>

[root@localhost templates]#


本文转自 gswljy 51CTO博客,原文链接:http://blog.51cto.com/guoshiwei/1900933


相关文章
|
1月前
|
设计模式 前端开发 数据库
Python Web开发:Django框架下的全栈开发实战
【10月更文挑战第27天】本文介绍了Django框架在Python Web开发中的应用,涵盖了Django与Flask等框架的比较、项目结构、模型、视图、模板和URL配置等内容,并展示了实际代码示例,帮助读者快速掌握Django全栈开发的核心技术。
186 45
|
1月前
|
开发框架 搜索推荐 数据可视化
Django框架适合开发哪种类型的Web应用程序?
Django 框架凭借其强大的功能、稳定性和可扩展性,几乎可以适应各种类型的 Web 应用程序开发需求。无论是简单的网站还是复杂的企业级系统,Django 都能提供可靠的支持,帮助开发者快速构建高质量的应用。同时,其活跃的社区和丰富的资源也为开发者在项目实施过程中提供了有力的保障。
|
1月前
|
API 数据库 开发者
深度剖析Django/Flask:解锁Web开发新姿势,让创意无限延伸!
在Web开发领域,Django与Flask如同两颗璀璨的星辰,各具特色。Django提供全栈解决方案,适合快速开发复杂应用;Flask则轻量灵活,适合小型项目和API开发。本文通过问答形式,深入解析两大框架的使用方法和选择策略,助你解锁Web开发新技能。
46 2
|
2月前
|
机器学习/深度学习 数据处理 数据库
基于Django的深度学习视频分类Web系统
基于Django的深度学习视频分类Web系统
66 4
基于Django的深度学习视频分类Web系统
|
1月前
|
安全 数据库 开发者
Python Web开发:Django框架下的全栈开发实战
【10月更文挑战第26天】本文详细介绍了如何在Django框架下进行全栈开发,包括环境安装与配置、创建项目和应用、定义模型类、运行数据库迁移、创建视图和URL映射、编写模板以及启动开发服务器等步骤,并通过示例代码展示了具体实现过程。
67 2
|
1月前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
Python Web框架比较:Django vs Flask vs Pyramid
44 1
|
2月前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
Python Web框架比较:Django vs Flask vs Pyramid
46 4
|
2月前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
【10月更文挑战第10天】本文比较了Python中三个最受欢迎的Web框架:Django、Flask和Pyramid。Django以功能全面、文档完善著称,适合快速开发;Flask轻量灵活,易于上手;Pyramid介于两者之间,兼顾灵活性和安全性。选择框架时需考虑项目需求和个人偏好。
40 1
|
2月前
|
机器学习/深度学习 监控 数据挖掘
基于Django和百度飞桨模型的情感识别Web系统
基于Django和百度飞桨模型的情感识别Web系统
47 5
|
2月前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
【10月更文挑战第6天】本文比较了Python中三个最受欢迎的Web框架:Django、Flask和Pyramid。Django功能全面,适合快速开发;Flask灵活轻量,易于上手;Pyramid介于两者之间,兼顾灵活性和可扩展性。文章分析了各框架的优缺点,帮助开发者根据项目需求和个人偏好做出合适的选择。
52 4