zoukankan      html  css  js  c++  java
  • Django速查学习复习

    1. 开端

    安装django :pip install django==2.1.0

    创建项目:django-admin startprojects A1

    启动项目:python manage.py runserver

    创建app:python startapp a1

    数据库迁移:python manage.py makemigrations

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

    2.settings.py 文件配置信息:

    INSTALLED_APPS :注册app。

    MIDDLEWARE:中间件,可以自定义中间件并在此注册,就可以执行方法(比如django的注册校验,权限校验)

    TEMPLATES:模板文件夹

    DATABASES:数据库配置(连接那种数据库)

    3.ORM的描述:

    orm(object reaction model)对象关系映射,用面向对象的方法来操纵数据库的技术

    优点:使用ORM更多的只用关注逻辑代码的编写,不用过多的考虑SQL语句

    提高开发效率

    缺点:

    会牺牲程序的执行效率

     

    对应关系:

    类 》》 数据表

    对象》》数据行

    属性》》字段

    4.定义视图函数的注意事项:
    函数第一个参数必须是request

    必须return一个对象

    5.FBV与CBV

    FBV:function based view 基于函数的视图

    CBV:class based view 基于类的视图

    6.CBV使用的装饰器:

    from django.utils.decorators import method_decorator

    方法:
    1.给方法上加装饰器:
        @method_decorator(warper)
        def get(self,request):
            return None
    2.给dispatch加上装饰器:
        @method_decorator(warper)
        def dispatch(self,request):
    3.给类加装饰器:
        @method_decorator(wraper,name='get')
        class Index(View):

    request对象方法和属性:
    属性:
        request.method 请求的方式8种:get,post,put,delete,options
        request.GET:字典 url上携带参数
        request.POST :字典 form表单通过post请求提交的数据
        request.path : 当前请求的路径
        request.body:请求体
        request.FILES:上传的文件
    方法:
        request.get_host() 主机地址
        request.get_full_path() URL路径 带参数

    视图中反向解析:
    from django.urls import reverse
        reverse('index') >> /idnex/
        无名分组:
        reverse('home',args=('1996','06')) >> '/home/1996/06/'
        有名分组:
        reverse('home',kwargs={'yesr':1996,'month':'06'})) >>> '/home/1996/06/'

    模板中反向解析:
    {% url 'index'%} >> /index/


    7.模板语法:
    变量:{{变量名}}
    过滤器:{{ 变量名 | filter:参数}}
    遍历:{% for i in name_list%}
    {{i}}
    {% endfor%}

     

  • 相关阅读:
    (最小生成树) 畅通工程再续 -- HDU --1875
    (最小生成树)Jungle Roads -- HDU --1301
    (最小生成树 )还是畅通工程 -- HDU--1233
    不同版本的 IIS 中使用 ASP.NET MVC(C#)【转】
    我们应当怎样做需求分析【转】
    C# DataTable几个常用的查询表达式【转】
    C# DataTable转实体 通用方法【转】
    C# 如何利用反射来加载程序集,并调用程序集中有关类的方法【转】
    IEnumerable和IEnumerator 详解 【转】
    循环对XML文档添加Attribute以及移除Element 【转】
  • 原文地址:https://www.cnblogs.com/cou1d/p/12242745.html
Copyright © 2011-2022 走看看