Django 系列博客(三)
Django 系列博客(三)
前言
本篇博客介绍 django 的前后端交互及如何处理 get 请求和 post 请求。
get 请求
get请求是单纯的请求一个页面资源,一般不建议进行账号信息的传输。
配置路由
from django.conf.urls import url
from django.contrib import admin
import app.views as app_views
import newApp.views as new_views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', app_views.home),
# 路由采用正则匹配, ^以什么开头 $以什么结果
# 注: 当路由没有子路由是,才在末尾添加$
url(r'^index/$', app_views.index),
url(r'login', app_views.login_action),
url(r'^new/index/$', new_views.index)
]
配置视图
from django.shortcuts import render, redirect
from django.http import HttpResponse
# Create your views here.
# 每一个请求,都对应一个视图响应函数,来出现请求,完成响应
# def index(abc):
# return HttpResponse('hello django') # 第一个响应
import django.core.handlers.wsgi
def login_action(request):
return render(request, 'login.html') # 第一个响应页面
# def home(request):
# return redirect('/index/') # 第一个重定向
def home(request):
return render(request, 'index.html')
def index(request):
return redirect('/')
配置页面资源
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>主页</title>
</head>
<body>
<h1 style="text-align: center">app的主页</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h1 style="color: red">登录</h1>
</body>
</html>
post 请求
配置路由
from django.conf.urls import url
from django.contrib import admin
from app import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home),
url(r'^index/$', views.index),
url(r'^login/$', views.login, name='lg'),
]
配置视图
from django.shortcuts import render, redirect
import pymysql
# Create your views here.
def home(request):
return render(request, 'index.html')
def index(request):
return redirect('/')
'''
def login(request):
print(request.method)
# 如果获取GET请求的提交数据
# import django.core.handlers.wsgi
# print(type(request))
# import django.http.request.QueryDict
# print(type(request.GET))
print(request.GET)
# usr = request.GET['usr'] # 不安全
usr = request.GET.get('usr', 'USR') # 安全, 第一个参数为数据的key, 第二个参数为默认值
print(usr)
pwd = request.GET.get('pwd') # 不设默认值,没有取到值时,返回值为None
print(pwd)
return render(request, 'login.html')
'''
from django.http import HttpResponse
def login(request):
if request.method == 'GET':
stus = request.GET.getlist('stu')
print(stus)
return render(request, 'login.html')
# 没有GET分支, 发来的请求为POST
usr = request.POST.get('usr')
pwd = request.POST.get('pwd')
print(usr, pwd)
# 连接数据库 => ORM
conn = pymysql.connect(host='localhost', port=3306, user='root', password='root', db='django')
cur = conn.cursor(pymysql.cursors.DictCursor)
# cur.execute('select * from user')
# users = cur.fetchall()
cur.execute('select * from user where usr=%s and pwd=%s', [usr, pwd])
res = cur.fetchone()
print(res)
if res != None:
return HttpResponse('登录成功')
return HttpResponse('登录失败')
配置页面资源
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>主页</title>
{# <link rel="stylesheet" href="./index.css">#}
{# <link rel="stylesheet" href="/static/index.css">#}
{# <link rel="stylesheet" href="/static/temp.css">#}
{# <link rel="stylesheet" href="/ooo/index.css">#}
{# <link rel="stylesheet" href="/ooo/temp.css">#}
{# <link rel="stylesheet" href="/static/css/test.css">#}
<link rel="stylesheet" href="/static/css/index.css">
</head>
<body>
<h1 style="text-align: center">主页</h1>
<img src="/static/img/001.png" alt="">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
<style>
.box {
border: 1px solid #ccc;
padding: 20px;
border-radius: 20px;
height: 380px;
}
</style>
</head>
<body>
{#<button class="btn btn-warning"> 按钮</button>#}
{#<div class="btn-group">#}
{# <button class="btn btn-default btn-lg dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true"#}
{# aria-expanded="false">#}
{# Large button <span class="caret"></span>#}
{# </button>#}
{# <ul class="dropdown-menu">#}
{# <li><a href="#">Action</a></li>#}
{# <li><a href="#">Another action</a></li>#}
{# <li><a href="#">Something else here</a></li>#}
{# <li role="separator" class="divider"></li>#}
{# <li><a href="#">Separated link</a></li>#}
{# </ul>#}
{#</div>#}
<div class="container">
<div class="box row col-sm-6 col-sm-offset-3">
{# action: 没写 | http://localhost:8801/login | /login/ | {% url 'url_name' %} #}
<form action="{% url 'lg' %}" method="GET">
{# {% csrf_token %}#}
<div class="form-group">
<label for="usr">用户名:</label>
<input type="text" class="form-control" name="usr" id="usr" placeholder="请输入用户名">
</div>
<div class="form-group">
<label for="pwd">Password</label>
<input type="password" class="form-control" name="pwd" id="pwd" placeholder="请输入密码">
</div>
<div class="checkbox">
<label>
<input name="stu" type="checkbox" value="stu1"> 学生1
</label>
<label>
<input name="stu" type="checkbox" value="stu2"> 学生2
</label>
<label>
<input name="stu" type="checkbox" value="stu3"> 学生3
</label>
</div>
<button type="submit" class="btn btn-info pull-right">登录</button>
</form>
</div>
</div>
{#<a href="/index/">前往主页</a>#}
</body>
<script src="/static/bootstrap-3.3.7-dist/js/jquery-3.3.1.js"></script>
<script src="/static/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
</html>
前后端交互
Django请求生命周期

Django 系列博客(三)的更多相关文章
- Django 系列博客(十六)
Django 系列博客(十六) 前言 本篇博客介绍 Django 的 forms 组件. 基本属性介绍 创建 forms 类时,主要涉及到字段和插件,字段用于对用户请求数据的验证,插件用于自动生成 h ...
- Django 系列博客(十三)
Django 系列博客(十三) 前言 本篇博客介绍 Django 中的常用字段和参数. ORM 字段 AutoField int 自增列,必须填入参数 primary_key=True.当 model ...
- Django 系列博客(九)
Django 系列博客(九) 前言 本篇博客介绍 Django 模板的导入与继承以及导入导入静态文件的几种方式. 模板导入 模板导入 语法:``{% include '模板名称' %} 如下: < ...
- Django 系列博客(八)
Django 系列博客(八) 前言 本篇博客介绍 Django 中的模板层,模板都是Django 使用相关函数渲染后传输给前端在显式的,为了想要渲染出我们想要的数据,需要学习模板语法,相关过滤器.标签 ...
- Django 系列博客(七)
Django 系列博客(七) 前言 本篇博客介绍 Django 中的视图层中的相关参数,HttpRequest 对象.HttpResponse 对象.JsonResponse,以及视图层的两种响应方式 ...
- Django 系列博客(六)
Django 系列博客(六) 前言 本篇博客介绍 Django 中的路由控制部分,一个网络请求首先到达的就是路由这部分,经过路由与视图层的映射关系再执行相应的代码逻辑并将结果返回给客户端. Djang ...
- Django 系列博客(四)
Django 系列博客(四) 前言 本篇博客介绍 django 如何和数据库进行交互并且通过 model 进行数据的增删查改 ORM简介 ORM全称是:Object Relational Mappin ...
- Django 系列博客(一)
Django 系列博客(一) 前言 学习了 python 这么久,终于到了Django 框架.这可以说是 python 名气最大的web 框架了,那么从今天开始会开始从 Django框架的安装到使用一 ...
- Django 系列博客(十四)
Django 系列博客(十四) 前言 本篇博客介绍在 html 中使用 ajax 与后台进行数据交互. 什么是 ajax ajax(Asynchronous Javascript And XML)翻译 ...
随机推荐
- MVC+EF 多条件查询
根据以前的做法是拼接sql语句,这会增加维护成本,因为sql语句里的内容不会报错,所以在使用ef的时候必须要抛弃拼接sql语句的习惯. 构建实例 List<vyw_user> list = ...
- OpenCV3.30 画图函数
画图函数(Draw Functions)都放在imgpro. 例如C++中用: #include <opencv2\imgproc.hpp>
- npm Error: Cannot find module './auth.js'
Mac 下升级 npm 到 v6.8.0 翻车. 提示: Error: Cannot find module './auth.js' 根据回显的报错路径,定位到这个文件中: npm/node_modu ...
- vector作为二维数组
vector本来就是可以用来代替一维数组的,vector提供了operator[]函数,可以像数组一样的操作,而且还有边界检查,动态改变大小. 这里只介绍用它来代替二维的数组,二维以上的可以依此类推. ...
- 蓝桥杯_算法训练_Torry的困惑(基本型)
这个题目就是求质数的乘积,在加一个模,思路比较简单,直接上代码: #include<iostream> using namespace std; bool isPrime(int a) { ...
- Android语音识别
语音识别 - 科大讯飞 开放平台 http://open.voicecloud.cn/ 需要拷贝lib.assets.并在清单文件中写一些权限 public class MainActivity ex ...
- nginx服务器入门知识汇总
IP-hash 就是根据IP进行hash计算,然后分配到对应的服务器,好处就是不用session同步,固定IP会固定访问一台服务器,缺点就是恶意攻击,会造成某台服务器压垮.提供的服务不同,面向的地区不 ...
- Ubuntu16.04下安装opencv3.4.2
1.安装官方给的opencv依赖包 GCC 4.4.x or later CMake 2.6 or higher Git GTK+2.x or higher, including headers (l ...
- 一文总结 Linux 虚拟网络设备 eth, tap/tun, veth-pair
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. Linux 虚 ...
- Python入门必学,用Python练习画个美队盾牌
0 环境 Python版本:3.6.6 操作系统:Mac OS Mojave 10.14.2 1 引言 最近我媳妇每天晚上吃饭时候也拿手机看,上厕所也在看. 看着看着还会笑?WTF?你在干嘛呢? 没错 ...