【Django】【二】模板
1. Django-bootstrap3
guest>python -m pip install django-bootstrap3
【代码】
settings.py
"""
Django settings for guest project. Generated by 'django-admin startproject' using Django 1.11.7. For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/ For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
""" import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'nvz+#19d=3x%-iqhyqpr@82^#1+bw_@j13k8m!id$oe%v-ecql' # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'sign',
'bootstrap3',
] MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
] ROOT_URLCONF = 'guest.urls' TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
] WSGI_APPLICATION = 'guest.wsgi.application' # Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST':'127.0.0.1',
'PORT':'3306',
'NAME':'guest',
'USER':'root',
'PASSWORD':'',
'OPTIONS':{
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
}
}
'''
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
''' # Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
] # Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/ STATIC_URL = '/static/' urls.py
"""guest URL Configuration The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from sign import views urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',views.index),
url(r'^index/$',views.index),
url(r'^accounts/login/$',views.index),
url(r'^login_action/$',views.login_action),
url(r'^event_manage/$',views.event_manage),
url(r'^search_name/$',views.search_name),
url(r'^guest_manage/$',views.guest_manage),
url(r'^sign_index/(?P<event_id>[0-9]+)/$',views.sign_index),
url(r'^sign_index_action/(?P<event_id>[0-9])/$',views.sign_index_action),
url(r'^logout/$',views.logout), ] views.py
from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse,HttpResponseRedirect
from sign.models import Event,Guest
from django.contrib.auth.decorators import login_required
from django.contrib import auth
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger # Create your views here.
#首页
def index(request):
return render(request, 'index.html')
#return HttpResponse('Hello Django!') '''
#cookie
#登录动作
def login_action(request):
if request.method == 'POST':
username = request.POST.get('username','')
password = request.POST.get('password','')
if username == 'admin' and password == 'admin123':
response = HttpResponseRedirect('/event_manage/')
response.set_cookie('user',username,3600) return response
else:
return render(request,'index.html',{'error':'username or password error!'})
else:
return render(request,'index.html',{'error':'username or password error!'}) #发布会管理
def event_manage(request):
username = request.COOKIES.get('user','') return render(request,'event_manage.html',{'user':username})
''' '''
#认证登录 使用 admin 管理后台创建用户账户来验证登录功能
def login_action(request):
if request.method == 'POST':
username = request.POST.get('username','')
password = request.POST.get('password','')
user = auth.authenticate(username=username,password=password)
if user is not None:
auth.login(request,user)
request.session['user'] = username
response = HttpResponseRedirect('/event_manage')
return response
else:
return render(request,'index.html',{'error':'username or password error!'})
else:
return render(request,'index.html',{'error':'username or password error!'}) '''
#session
#登录动作
def login_action(request):
if request.method == 'POST':
username = request.POST.get('username','')
password = request.POST.get('password','')
if username == 'admin' and password == 'admin123':
response = HttpResponseRedirect('/event_manage')
request.session['user'] = username
return response else:
return render(request,'index.html',{'error':'username or password error!'})
else:
return render(request,'index.html',{'error':'username or password error!'}) #发布会管理
#@login_required #【备注】不注释就总是跳到登录页,不知道为啥
def event_manage(request):
event_list = Event.objects.all()
username = request.session.get('user','')
return render(request,'event_manage.html',{'user':username,'events':event_list}) #发布会名称搜索
#@login_required
def search_name(request):
username = request.session.get('user','')
search_name = request.GET.get('name','')
event_list = Event.objects.filter(name__contains=search_name)
return render(request,'event_manage.html',{'user':username,'events':event_list}) #嘉宾管理
#@login_required
def guest_manage(request):
username = request.session.get('user','')
guest_list = Guest.objects.all()
paginator = Paginator(guest_list,2)
page = request.GET.get('page')
try:
contacts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer,deliver first page.
contacts = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g.9999),deliver last page os results.
contacts = paginator.page(paginator.num_pages)
return render(request,'guest_manage.html',{'user':username,'guests':contacts}) #签到页面
#@login_required
def sign_index(request,event_id):
event = get_object_or_404(Event,id=event_id)
return render(request,'sign_index.html',{'event':event}) #签到功能
#@login_required
def sign_index_action(request,event_id):
event = get_object_or_404(Event,id=event_id)
phone = request.POST.get('phone','')
result = Guest.objects.filter(phone=phone)
if not result:
return render(request,'sign_index.html',{'event':event,'hint':'phone error.'})
result = Guest.objects.filter(phone=phone,event_id=event_id)
if not result:
return render(request,'sign_index.html',{'event':event,'hint':'event id or phone error.'})
result = Guest.objects.get(phone=phone,event_id=event_id)
if result.sign:
return render(request,'sign_index.html',{'event':event,'hint':'user has sign in.'})
else:
Guest.objects.filter(phone=phone,event_id=event_id).update(sign = '1')
return render(request,'sign_index.html',{'event':event,'hint':'sign in success','guest':result}) #退出登录
#@login_required
def logout(request):
auth.logout(request)
response = HttpResponseRedirect('/index/')
return response templates/event_manage.html
<html lang="zh-CN">
<head>
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript %}
<title>Guest Manage</title>
</head>
<body role="document">
<!-- 导航栏 -->
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/event_manage/">Guest Manage System</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">发布会</a></li>
<li><a href="/guest_manage/">嘉宾</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">{{user}}</a></li>
<li><a href="/logout/">退出</a></li>
</ul>
</div>
</div>
</nav> <!--发布会搜索表单-->
<div class="page-header" style="padding-top:60px;">
<div id="navbar1" class="navbar-collapse collapse">
<form class="navbar-form" method="get" action="/search_name/">
<div class="form-group">
<input name="name" type="text" placeholder="名称" class="form-control">
</div>
</form>
</div> </div> <!-- 发布会列表 -->
<div class="row" style="padding-top: 80px;">
<div class="col-md-6">
<table class="table table-striped">
<thead>
<tr>
<th>id</th><th>名称</th><th>状态</th><th>地址</th><th>时间</th><th>签到</th>
</tr>
</thead>
<tbody>
{% for event in events %}
<tr>
<td>{{ event.id }}</td>
<td>{{ event.name }}</td>
<td>{{ event.status }}</td>
<td>{{ event.address }}</td>
<td>{{ event.start_time }}</td>
<td>
<a href="/sign_index/{{event.id}}/" target="{{event.id}}_blank">sign</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</body>
</html> templates/guest_manage.html
<!DOCTYPE html>
<html lang="en">
<head>
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript %}
<title>Guest Manage</title>
</head>
<body role="document">
<!- 导航栏 -->
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/event_manage/">Guest Manage System</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="/event_manage/">发布会</a></li>
<li class="active"><a href="#">嘉宾</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">{{user}}</a></li>
<li><a href="/logout/">退出</a></li>
</ul>
</div> </div>
</nav> <!-- 嘉宾列表 -->
<div class="row" style="padding-top: 80px;">
<div class="col-md-6">
<table class="table table-striped">
<thead>
<tr>
<th>id</th>
<th>名称</th>
<th>手机</th>
<th>Email</th>
<th>签到</th>
<th>发布会</th>
</tr> </thead>
<tbody>
{% for guest in guests %}
<tr>
<td>{{guest.id}}</td>
<td>{{guest.realname}}</td>
<td>{{guest.phone}}</td>
<td>{{guest.email}}</td>
<td>{{guest.sign}}</td>
<td>{{guest.event}}</td> </tr>
{% endfor %}
</tbody>
</table>
</div>
</div> <!-- 列表分页器 -->
<div class="pagination">
<span class="step-links">
{% if guests.has_previous %}
<a href="?page={{ guests.prevous_page_number}}">previous</a>
{% endif %}
<span class="current">
Page {{guests.number}} of {{guests.paginator.num_pages}}.
</span>
{% if guests.has_next %}
<a href="?page={{ guests.next_page_number }}">next</a>
{% endif %}
</span>
</div>
</body>
</html> index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django Page</title>
</head>
<body>
<h1>发布会管理</h1>
<form method="post" action="/login_action/">
<input name="username" type="text" placeholder="username"><br>
<input name="password" type="password" placeholder="password"><br>
{{ error }}<br>
<button id="bth" type="submit">登录</button> </form>
</body>
</html> templates/sign_index.html
<!DOCTYPE html>
<html lang="en">
<head>
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript %}
<title>签到</title>
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">{{ event.name }}</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="/event_manage/">发布会</a></li>
<li><a href="/guest_manage/">嘉宾</a></li>
</ul>
</div>
</div>
</nav> <!-- 签到功能 -->
<div class="page-header" style="padding-top: 80px;">
<div id="navbar1" class="navbar-collapse collapse">
<form class="navbar-form" method="post" action="/sign_index_action/{{event.id}}/">
<div class="form-group">
<input name="phone" type="text" placeholder="输入手机号" class="form-control">
</div>
<button type="submit" class="btn btn-success">签到</button>
<font color="red">
<br>{{ hint}}
<br>{{ guest.realname }}
<br>{{ guest.phone }}
</font>
</form>
</div>
</div>
</body>
</html>
【Django】【二】模板的更多相关文章
- Django(二)模板
一.模板概念 1.Django通过模板动态生成html 2.模板的加载位置 模板一般建立在templates文件夹中,全局路径的设置在settings.py中 DIRS:决定了整个项目的模板路径的 ...
- django的模板(二)
模板(二) 实验简介 本节继续介绍模板的常用标签,for.if.ifequal和注释标签. 一.基本的模板标签和过滤器 1. 标签 if/else {% if %} 标签检查(evaluate)一个变 ...
- python学习笔记--Django入门二 Django 的模板系统
为了使网站更干净简洁更容易维护,页面的设计和Python的代码必须分离开.我们可以使用Django的 模板系统 (Template System)来实现这种模式. 几个简单的模板标签(tag): ...
- Django的模板系统
一.语法 关于模板渲染只需要记住两种特殊符号(语法): {{ }} 和 {% %} (变量相关用{{ }} 逻辑相关用{% %}) 二.变量 在Django的模板语言中按照{{ 变量名 }}来使用 ...
- Django的模板层
一 模版简介 你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now ...
- Django之模板1
Django模板 一.变量(只需要记住两种特殊符号) {{ }} 和{% %} {{ }}表示变量,在模板渲染的时候替换成值,变量名由字母数字和下划线组成. {% %}表示逻辑相关的操作. 点(.)在 ...
- python 全栈开发,Day69(Django的视图层,Django的模板层)
昨日内容回顾 相关命令: 1 创建项目 django-admin startproject 项目名称 2 创建应用 python manage.py startapp app名称 3 启动项目 pyt ...
- Django框架----模板语法
Django模板系统 官方文档 一.什么是模板? 只要是在html里面有模板语法就不是html文件了,这样的文件就叫做模板. 二.模板语法分类 只需要记两种特殊符号: {{ }}和 {% %} 变量 ...
- Django之模板语法
Django框架之第三篇模板语法(重要!!!) 一.什么是模板? 只要是在html里面有模板语法就不是html文件了,这样的文件就叫做模板. 二.模板语法分类 一.模板语法之变量:语法为 {{ }}: ...
- Django框架-模板系统
来看一段代码 def current_datetime(request): now = datetime.datetime.now() html = "<html><bod ...
随机推荐
- 阻塞队列---ArrayBlockingQueue,LinkedBlockingQueue,DelayQueue源码分析
阻塞队列和非阻塞队列阻塞队列和非阻塞队列的区别:阻塞队列可以自己阻塞,非阻塞队列不能自己阻塞,只能使用队列wait(),notify()进行队列消息传送.而阻塞队列当队列里面没有值时,会阻塞直到有值输 ...
- linux利用scp远程上传下载文件/文件夹
scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度. 当你服务 ...
- 五一 DAY2
DAY 2 比如:依次输入 3 1 5 4 2 插入 6 这里FZdalao有一个很巧妙的构造,直接吧输入的数字排成一个二 ...
- django自定义错误响应
在做一个web时,总是会出现各种错误,如400.403.404.500等.一般开发都要做对应的处理,给一些友好提示,或返回一些公益广告等. 在Django中,默认提供了常见的错误处理方式,比如: ha ...
- Linux使用退格键时出现^H解决方法
以前在linux下执行脚本不注意输错内容需要删除时总是出现^H ,以前不知道真相的我没办法只有再重头运行一次脚本,后来发现其实时有解决办法的,所以记录一下. ^H不是H键的意思,是backspace. ...
- linux服务器启动报错UNEXPECTED INCONSISTENCY解决方法
内网的linux服务器给开发员用来测试以及共享文件使用,今天早上发现xshell连接不上该服务器,一开始进入系统显示reboot and select proper boot device or in ...
- 20145208 蔡野 《网络对抗》免考项目 MSF学习
20145208 蔡野 <网络对抗>免考项目 MSF Exploit模块开发 题目:MSF Exploit模块开发 摘要 本免考项目的目标是通过对msf模块的设计语言--ruby和expl ...
- shelve模块,sys模块,logging模块
1.shelve模块 用于序列化的模块,shelve模块比pickle模块简单,只有open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型. impor ...
- Docker基础学习-尚硅谷
视频地址:链接: https://pan.baidu.com/s/15sJuEh5cVTJ7-vWSH7vffw 提取码: zf25 笔记:
- linux内核中的vgaarb是什么?
答: vga仲裁器(vga arbiter),是内核中的一个模块