zoukankan      html  css  js  c++  java
  • 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


  • 相关阅读:
    关于vs的网站发布
    gaga...
    将gridView中的数据导出 并保存到excel中
    ASP.NET 2.0 Treeview Checkboxes Check All Javascript
    写一个自动编号的存储过程
    获取一个部门的所有下级部门
    相同则写入组合
    jquery html form
    repeater合并单元格
    VS2010 快捷键大全
  • 原文地址:https://www.cnblogs.com/junyingwang/p/9809708.html
Copyright © 2011-2022 走看看