In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces Django how to achieve WEB login, the article is very detailed, has a certain reference value, interested friends must read it!
Implementation environment:
1 、 System version:rh7.5
2 、 Python version:2.6.6
3 、 Django version:1.2.7
Create a project:
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
File configuration:
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 p_w_picpaths. 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! (0b (^ 8hb0w')
# 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
Title
Index
Username:
Password:
`alert'
`count'
[root@localhost templates] #
Success.html
[root@localhost templates] # cat success.html
Title
Index
Welcome
[root@localhost templates] #
The above is all the contents of the article "how to achieve WEB login in Django". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.