当前位置: 首页 > news >正文

day-19 django2

day19

1. Django的命令
1. 下载安装
pip install django==1.11.15

2. 创建项目
django-admin startproject 项目名

3. 创建APP
cd 项目目录下
python manage.py startapp app01

4. 启动项目
python manage.py runserver # 127.0.0.1:8000
python manage.py runserver 0.0.0.0:80
python manage.py runserver 80 # 127.0.0.1:80

5. 数据库相关
python manage.py makemigrations # 检测model的变化 把变更记录记录下来
python manage.py makemigrations app01 # 检测model的变化 把变更记录记录下来

python manage.py migrate # 将变更记录同步到数据库中


2. setting的配置
1.
INSTALLED_APPS=[
'app01.apps.App01Config',
'app01'
]

2. TEMPLATES
'DIRS': [os.path.join(BASE_DIR, 'templates')]

3. DATABASES

ENGINE mysql
NAME: 数据库的名字
USER: 用户名
PASSWORD: 密码
HOST: '127.0.0.1'
PORT: 3306

注意:
在与settins同级目录下的__init__.py 中写代码:
import pymysql
pymysql.install_as_MySQLdb()

4. 静态文件相关
STATIC_URL = '/static/' # 别名
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static'),
]


1. 今日内容

1. 路由系统进阶:https://www.cnblogs.com/maple-shaw/articles/9282718.html
- 动态路由
- URL命名和反向解析
url(r'^publisher_list/$', views.publisher_list, name='publisher'),


命名分组
url(r'^pub/edit/(?P<pk>\d+)/$', views.publisher_edit,name='publisher_edit'),

模板中使用:
{% url 'publisher'%} ——》/app01/publisher_list/

命名分组
{% url 'publisher_edit' publisher.id %}
{% url 'publisher_edit' pk=publisher.id %}

视图中使用:
from django.urls import reverse
reverse('publisher')

命名分组
reverse('publisher_edit',args=(2,))
reverse('publisher_edit',kwargs={'pk':5})


指定namespace='app01'
反向解析的时候 给name前面加上namespace ——》 namespace:name



2. 视图函数进阶: https://www.cnblogs.com/maple-shaw/articles/9285269.html
- FBV (function based view)


- CBV (class based view)

定义:
class AddPublisher(View):

def get(self,request):
pass

def post(self,request):
pass

使用:
url(r'^pub/add/$', views.AddPublisher.as_view(),name='publisher_add'),


CBV的流程
1. AddPublisher.as_view() ——》 view函数
2. 请求到来的时候 执行view函数
1. AddPublisher实例化 对象 ——》 self
2. self.request = request
3. 执行self.dispatch(request, *args, **kwargs)
1. 通过反射 获取到 get post 方法 ——>handler

2. handler() ——》 获得HttpResponse对象

装饰器的使用:
FBV: 正常使用 给函数上加装饰器
CBV:
from django.utils.decorators import method_decorator

1. 加在方法上
@method_decorator(timer)
def get(self, request):

2. 加在dispatch方法上
@method_decorator(timer)
def dispatch(self, request, *args, **kwargs):

3. 加在类上
@method_decorator(timer,name='post')
@method_decorator(timer,name='get')
class AddPublisher(View):

1. 直接用装饰器
(<app01.views.AddPublisher object at 0x0000023DE99E57B8>, <WSGIRequest: GET '/app01/pub/add/'>)
<function AddPublisher.get at 0x0000023DE992ED08>
2. 使用method_decorator
(<WSGIRequest: GET '/app01/pub/add/'>,)
<function method_decorator.<locals>._dec.<locals>._wrapper.<locals>.bound_func at 0x0000023372154EA0>

- request
request.method 请求方式 GET POST
request.GET URL上的参数
request.POST POST请求提交的数据
request.path_info 路径信息 不包含ip和端口 不包含URL参数 /app01/pub_list/
request.FILES 上传的文件

request.get_host() IP:端口
request.get_full_path() 带参数的路径

- response
HttpResponse('字符串')
render(request,'html文件名',{}) 完整的页面
redirect(地址) '/zz/' 'https://v3.bootcss.com/css/#forms' Location :'/zz/'

from django.http import JsonResponse
JsonResponse # Content-Type: application/json
JsonResponse(data_list,safe=False) 传非字典类型


模板引擎进阶: https://www.cnblogs.com/maple-shaw/articles/9333821.html
- 标签
- 过滤器
- 模板

1. 把多个页面公共的部分提取出来 写成一个母版 HTML文件 'base.html'
定义block

2. 写子页面
{% extends 'base.html' %}
重写母版中定义的block块

3. 注意事项:
1. {% extends 'base.html' %}写在第一行,'base.html'是字符串
2. 在母版中定义多个block块 css js
3. 子页面的内容写在block中

- CSRF
CSRF 跨站请求伪造
Django有跨站请求伪造的保护机制 依赖中间件
'django.middleware.csrf.CsrfViewMiddleware',

{% csrf_token %}

效果:
在form表中加了一个隐藏的input标签 name csefmiddlewaretoken value 64长度的字符串
结果:
form表单可以提交POST请求

- 静态文件相关
- {% load static %}

<link rel="stylesheet" href="{% static 'bootstrap-3.3.7/css/bootstrap.css' %}"
<link rel="stylesheet" href="{% static 'css/dsb.css' %}">}

<link rel="stylesheet" href="{% get_static_prefix %}bootstrap-3.3.7/css/bootstrap.css">
<link rel="stylesheet" href="{% get_static_prefix %}css/dsb.css">

ORM单表查询13条+外键操作(一对多):
字段和参数: https://www.cnblogs.com/maple-shaw/articles/9323320.html
查询操作:https://www.cnblogs.com/maple-shaw/articles/9403501.html

cookie&session:https://www.cnblogs.com/maple-shaw/articles/9502602.html

 

 


转载于:https://www.cnblogs.com/junyingwang/p/9809708.html

相关文章:

  • Go 语言编译器的 //go: 详解
  • 《2019年世界发展报告》发布,阿里巴巴助力小企业发展创造就业
  • @Service注解让spring找到你的Service bean
  • python 3.5 解决csv 读入中的'utf-8' codec can't decode办法
  • 2018 JVM 生态报告:79% 的 Java 开发者使用 Java 8
  • 微信小程序 - 使用七牛云 API 截取第 n 秒图像为封面图
  • 《netty入门与实战》笔记-03:数据传输载体 ByteBuf 介绍
  • 【转】使用 lsof 查找打开的文件
  • 实验报告五201521460014 综合渗透
  • EDMA3浅析
  • Lua与C/C++的交互
  • java网络编程之IO
  • 最好用的中间人***工具mitmproxy
  • linuxcentos忘记root管理用户密码 单用户模式维护重置密码操作指引
  • go语言学习初探(一)
  • ----------
  • 【391天】每日项目总结系列128(2018.03.03)
  • 【附node操作实例】redis简明入门系列—字符串类型
  • Akka系列(七):Actor持久化之Akka persistence
  • Docker: 容器互访的三种方式
  • ESLint简单操作
  • Flex布局到底解决了什么问题
  • IDEA 插件开发入门教程
  • javascript 总结(常用工具类的封装)
  • JS创建对象模式及其对象原型链探究(一):Object模式
  • leetcode388. Longest Absolute File Path
  • Python打包系统简单入门
  • 当SetTimeout遇到了字符串
  • 对象管理器(defineProperty)学习笔记
  • 如何选择开源的机器学习框架?
  • 如何抓住下一波零售风口?看RPA玩转零售自动化
  • 微信小程序开发问题汇总
  • 小程序 setData 学问多
  • 学习笔记:对象,原型和继承(1)
  • 验证码识别技术——15分钟带你突破各种复杂不定长验证码
  • [地铁译]使用SSD缓存应用数据——Moneta项目: 低成本优化的下一代EVCache ...
  • ​3ds Max插件CG MAGIC图形板块为您提升线条效率!
  • ​ubuntu下安装kvm虚拟机
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (规划)24届春招和25届暑假实习路线准备规划
  • (三)Honghu Cloud云架构一定时调度平台
  • (转)视频码率,帧率和分辨率的联系与区别
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • .NET Compact Framework 3.5 支持 WCF 的子集
  • .NET(C#) Internals: as a developer, .net framework in my eyes
  • .net和php怎么连接,php和apache之间如何连接
  • /proc/interrupts 和 /proc/stat 查看中断的情况
  • [100天算法】-目标和(day 79)
  • [Angular] 笔记 21:@ViewChild
  • [Angular] 笔记 6:ngStyle
  • [BZOJ2208][Jsoi2010]连通数
  • [C#]扩展方法
  • [caffe(二)]Python加载训练caffe模型并进行测试1
  • [CodeForces-759D]Bacterial Melee
  • [CSS]文字旁边的竖线以及布局知识