django版本:1.4.21。
一、准备工作
1、新建项目和app
[root@yl-web-test srv]# django-admin.py startproject lxysite [root@yl-web-test srv]# cd lxysite/ [root@yl-web-test lxysite]# python manage.py startapp accounts [root@yl-web-test lxysite]# ls accounts lxysite manage.py
2、配置app
在项目settings.py中的
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', # Uncomment the next line to enable the admin: # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', 'accounts', )
3、配置url
在项目urls.py中配置
urlpatterns = patterns('', # Examples: # url(r'^$', 'lxysite.views.home', name='home'), # url(r'^lxysite/', include('lxysite.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: # url(r'^admin/', include(admin.site.urls)), url(r'^accounts/', include('accounts.urls')), )
4、配置templates
新建templates目录来存放模板,
[root@yl-web-test lxysite]# mkdir templates [root@yl-web-test lxysite]# ls accounts lxysite manage.py templates
然后在settings中配置
TEMPLATE_DIRS = ( # 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. '/srv/lxysite/templates', )
5、配置数据库
我用的是mysql数据库,先创建数据库lxysite
mysql> create database lxysite; Query OK, 1 row affected (0.00 sec)
然后在settings.py中配置
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'lxysite', # Or path to database file if using sqlite3. 'USER': 'root', # Not used with sqlite3. 'PASSWORD': 'password', # Not used with sqlite3. 'HOST': '10.1.101.35', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '3306', # Set to empty string for default. Not used with sqlite3. } }
然后同步数据库:同步过程创建了一个管理员账号:liuxiaoyan,password,后面就用这个账号登录和注销登录。
[root@yl-web-test lxysite]# python manage.py syncdb Creating tables ... Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_user_permissions Creating table auth_user_groups Creating table auth_user Creating table django_content_type Creating table django_session Creating table django_site You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now"color: #ff0000">二、实现登录功能使用django自带的用户认证,实现用户登录和注销。
1、定义一个用户登录表单(forms.py)
因为用的了bootstrap框架,执行命令
#pip install django-bootstrap-toolkit
安装。并在settings.py文件中配置
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', # Uncomment the next line to enable the admin: # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', 'bootstrap_toolkit', 'accounts', )forms.py没有强制规定,建议放在和app的views.py同一目录。
#coding=utf-8 from django import forms from django.contrib.auth.models import User from bootstrap_toolkit.widgets import BootstrapDateInput,BootstrapTextInput,BootstrapUneditableInput class LoginForm(forms.Form): username = forms.CharField( required = True, label=u"用户名", error_messages={'required':'请输入用户名'}, widget=forms.TextInput( attrs={ 'placeholder':u"用户名", } ) ) password = forms.CharField( required=True, label=u"密码", error_messages={'required':u'请输入密码'}, widget=forms.PasswordInput( attrs={ 'placeholder':u"密码", } ), ) def clean(self): if not self.is_valid(): raise forms.ValidationError(u"用户名和密码为必填项") else: cleaned_data = super(LoginForm,self).clean()定义的登录表单有两个域username和password,这两个域都为必填项。
2、定义登录视图views.py
在视图里实例化上一步定义的用户登录表单
# Create your views here. from django.shortcuts import render_to_response,render,get_object_or_404 from django.http import HttpResponse, HttpResponseRedirect from django.contrib.auth.models import User from django.contrib import auth from django.contrib import messages from django.template.context import RequestContext from django.forms.formsets import formset_factory from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage from bootstrap_toolkit.widgets import BootstrapUneditableInput from django.contrib.auth.decorators import login_required from forms import LoginForm def login(request): if request.method == 'GET': form = LoginForm() return render_to_response('login.html',RequestContext(request,{'form':form,})) else: form = LoginForm(request.POST) if form.is_valid(): username = request.POST.get('username','') password = request.POST.get('password','') user = auth.authenticate(username=username,password=password) if user is not None and user.is_active: auth.login(request,user) return render_to_response('index.html',RequestContext(request)) else: return render_to_response('login.html',RequestContext(request,{'form':form,'password_is_wrong':True})) else: return render_to_response('login.html',RequestContext(request,{'form':form,}))该视图实例化了前面定义的LoginForm,它的主要业务流逻辑是:
1、判断必填项用户名和密码是否为空,如果为空,提示“用户名和密码为必填项”的错误信息。
2、判断用户名和密码是否正确,如果错误,提示“用户名或密码错误”的错误信息。
3、登录成功后,进入主页(index.html)
3、登录页面模板(login.html)
<!DOCTYPE html> {% load bootstrap_toolkit %} {% load url from future %} <html lang="en"> <head> <meta charset="utf-8"> <title>数据库脚本发布系统</title> <meta name="description" content=""> <meta name="author" content="朱显杰"> {% bootstrap_stylesheet_tag %} {% bootstrap_stylesheet_tag "responsive" %} <style type="text/css"> body { padding-top: 60px; } </style> <!--[if lt IE 9]> <script src="/UploadFiles/2021-04-08/html5.js">配置accounts的urls.py
from django.conf.urls import * from accounts.views import login,logout urlpatterns = patterns('', url(r'login/$',login), )4、首页(index.html)
代码如下:
<!DOCTYPE html> {% load bootstrap_toolkit %} <html lang="en"> {% bootstrap_stylesheet_tag %} {% bootstrap_stylesheet_tag "responsive" %} <h1>登录成功</h1> <a href="/accounts/logout/"><input type="button" value="登出" class="btn btn-success"></a> </html>配置登出的url
from django.conf.urls import * from accounts.views import login,logout urlpatterns = patterns('', url(r'login/$',login), url(r'logout/$',logout), )登录视图如下:调用djagno自带用户认证系统的logout,然后返回登录界面。
@login_required def logout(request): auth.logout(request) return HttpResponseRedirect("/accounts/login/")上面@login_required标示只有登录用户才能调用该视图,否则自动重定向到登录页面。
三、登录注销演示
1、执行python manage.py runserver 0.0.0.0:8000
在浏览器输入ip+端口访问,出现登录界面
2、当用户名或密码为空时,提示“用户名和密码为必填项”
3、当用户名或密码错误时,提示“用户名或密码错误”
4、输入正确用户名和密码(创建数据库时生成的liuxiaoyan,password),进入主页
5、点击登出,注销登录,返回登录页面。
四、排错
1、'bootstrap_toolkit' is not a valid tag library
因为你的INSTALLED_APP没有安装'bootstrap_toolkit',安装即可。
资源链接
https://www.jb51.net/article/143857.htm
https://www.jb51.net/article/143850.htm
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 《丁当 20首重量级歌曲 Fu Good 下一站天后 2CD》[WAV/分轨][650MB]
- 歌莉雅.2012-My.Voice.Story【环星】【FLAC分轨】
- 群星.1993-一曲成名·青春无悔【飞碟】【WAV+CUE】
- 刘德华.2000-爱无知(金装版)【NEWMELODY】【WAV+CUE】
- 许巍《时光漫步》1CD[FLAC/分轨][804.3MB]
- 群星《2024第一季度百度歌曲排行抖音榜top100》1CD[FLAC/分轨][2.3GB]
- 张柏芝《全新经验》[FLAC/分轨][450MB]
- 唐朝.2008-浪漫骑士【音乐家】【WAV+CUE】
- 张芸京.2016-失败的高歌【泡耳音乐】【WAV+CUE】
- 群星.1991-音乐工厂1·皇后大道东【音乐工厂】【WAV+CUE】
- 群星.1992-音乐工厂2·首都【音乐工厂】【WAV+CUE】
- 群星.1994-音乐工厂3·儿童乐园【音乐工厂】【WAV+CUE】
- 胡鸿钧.2021-ex:CHANGE【星梦娱乐】【WAV+CUE】
- 班得瑞原装进口《第一张新世纪专辑:仙境》1CD[APE/CUE分轨][292.3MB]
- 伍佰《摇滚教父 伍佰黄金精选 2CD》[WAV+CUE][990MB]