django(4)

First, the internal principle of render

1. The role of render : return an html page; and can also pass data to the page.

2. The internal principle of render (the following code)

from django.template import Template,Context
def index(request):
    temp = Template('<h1>{
     
     { user }}</h1>')
    con = Context({"user":{"name":'jason',"password":'123'}})
    res = temp.render(con)
    print(res) # <h1>{&#39;name&#39;: &#39;hh&#39;, &#39;password&#39;: &#39;123&#39;}</h1>
    return HttpResponse(res)

Replenish:

# FBV,自动给后面index加()执行
url(r'^index/',views.index),

2. FBV and CBV

FBV (Function-Based View) ------> Function

CBV (class-based view) ————” class

First import the module: views view

from django.views import View

Small conclusion: FBV and CBV are consistent in routing matching. They are all url followed by the memory address of the function

# CBV的URL部分
url(r'^login/',views.MyLogin.as_view()),# 括号优先级最高!!!!
# 通过源码得出:(上下等价)括号优先级最高!!!!
url(r'^login/',views.view) # FBV和CBV在路由匹配上是一致的 都是url+函数的内存地址

# CBV的view视图函数部分
class MyLogin(View):
    def get(self,request):
        return render(request,'login.html')
    def post(self,request):
        return HttpResponse("from MyLogin post方法")

# CBV的html部分
<form action="" method='post'> # post注意点:去掉中间件
    username: <input type="text" name="username">
    <input type="submit">

Regarding post, remove middleware

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',
]

Question: When a get request comes, it will go to the get method in the class, and if a post request comes, it will go to the post method in the class. Why???

Research direction
1. Start with url

url(r'^login/', views.MyLogin.as_view()) Since the function name and parentheses have the highest execution priority, the as_view() method will be executed immediately after this sentence is written

@classonlymethod
def as_view(cls, **initkwargs):  # cls就是我们自己的写的类 MyLogin
    def view(request, *args, **kwargs):
        self = cls(**initkwargs)  # 实例化产生MyLogin的对象  self = MyLogin(**ininkwargs)
        if hasattr(self, 'get') and not hasattr(self, 'head'):
            self.head = self.get
            self.request = request
            self.args = args
            self.kwargs = kwargs
            # 上面的几句话都仅仅是在给对象新增属性
            return self.dispatch(request, *args, **kwargs)  # dispatch返回什么 浏览器就会收到什么
        # 对象在查找属性或者方法的时候 你一定要默念 先从对象自己这里找  然后从产生对象的类里面找  最后类的父类依次往后
        return view

    通过源码发现url匹配关系可以变形成
    url(r'^login/',views.view)  # FBV和CBV在路由匹配上是一致的 都是url后面跟函数的内存地址
    2.当浏览器中输入login 会立刻触发view函数的运行
    def dispatch(self, request, *args, **kwargs):
        # Try to dispatch to the right method; if a method doesn't exist,
        # defer to the error handler. Also defer to the error handler if the
        # request method isn't on the approved list.
        # 我们先以GET为例
        if request.method.lower() in self.http_method_names:  # 判断当前请求方法是否在默认的八个方法内
            # 反射获取我们自己写的类产生的对象的属性或者方法
            # 以GET为例  handler = getattr(self,'get','取不到报错的信息')
            # handler = get(request)
            handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
            else:
                handler = self.http_method_not_allowed
                return handler(request, *args, **kwargs)  # 直接调用我们自己的写类里面的get方法
            # 源码中先通过判断请求方式是否符合默认的八个请求方法 然后通过反射获取到自定义类中的对应的方法执行

Three, django settings source code

Projects usually have two configuration files: one is exposed to the user and can be configured; the other is the default configuration of the project.

3.1 Features: When the user is configured, the user's default is used, and when the user is not configured, the project default is used.

3.2 File location:

1.django中暴露给用户的配置默认就与项目同名的文件夹中

2.django项目内部默认的配置文件
from django.conf import global_settings

3.1 Question : Why APPEND_SLASH=False must use uppercase, lowercase is invalid

premise:

1.(两个配置文件)django除了暴露给用户一个settings.py配置文件之外  自己内部还有一个全局的配置文件
2.我们在使用配置文件的时候 可以直接直接导入暴露给用户的settings.py也可以使用django全局的配置文件 并且后者居多
from django.conf import settings
3.django的启动入口是manage.py 

3.4 Usually, the configuration files we use in django are generally imported in the following way

from django. conf import settings
import os
import sys

if __name__ == "__main__":
    # django在启动的时候 就会往全局的大字典中设置一个键值对  值是暴露给用户的配置文件的路径字符串
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day54.settings")

    class Settings(object):
        def __init__(self, settings_module):  # settings_module = 'day54.settings'
            # update this dict from global settings (but only for ALL_CAPS settings)
            for setting in dir(global_settings):  # django全局配置文件
                # dir获取django全局配置文件中所有的变量名
                if setting.isupper():  # 判断文件中的变量名是否是大写 如果是大写才会执行/生效
                    setattr(self, setting, getattr(global_settings, setting))  # 给settings对象设置键值对
                    # 给settings对象设置键值对  settings[配置文件中大写的变量名] = 配置文件中大写的变量名所对应的值

                    # store the settings module in case someone later cares
                    self.SETTINGS_MODULE = settings_module  # 'day54.settings'

                    mod = importlib.import_module(self.SETTINGS_MODULE)  # mod = 模块settings(暴露给用户的配置文件)
                    for setting in dir(mod):  # for循环获取暴露给用户的配置文件中所有的变量名
                        if setting.isupper():  # 判断变量名是否是大写
                            setting_value = getattr(mod, setting)  # 获取大写的变量名所对应的值
                            setattr(self, setting, setting_value)  # 给settings对象设置键值对
                            """
                            d = {}
                            d['username'] = 'jason'
                            d['username'] = 'egon'
                            用户如果配置了就用用户的
                            用户如果没有配置就用系统默认的
                            其实本质就是利用字典的键存在就是替换的原理 实现了用户配置就用用户的用户没配置就用默认的
                            """

                            class LazySettings(LazyObject):
                                def _setup(self, name=None):
                                    # os.environ你可以把它看成是一个全局的大字典
                                    settings_module = os.environ.get(ENVIRONMENT_VARIABLE)  # 从大字典中取值键为DJANGO_SETTINGS_MODULE所对应的值:day54.settings
                                    # settings_module = 'day54.settings'
                                    self._wrapped = Settings(settings_module)  # Settings('day54.settings')


                                    settings = LazySettings()  # 单例模式   

Refer to the settings source code to write an example (must know!):

Fourth, the template layer

4.1 Classification of html template syntax:

1.{
     
     {}}  变量相关;在html页面中获取后端的值
2.{%%}  逻辑相关

4.2render is two ways of passing the value of the page.

render can return an html page and pass data for the page. The following are two ways of writing by value.

#1字典传输法
return render(request,'reg.html',{'n':n,'f':f})# 通过字典的键值对 指名道姓的一个个的传。如果这个函数数据特别多,一个一个k,v键值对的写,很累!

#2等价写法locals()
return render(request,'reg.html',locals())#Locals(),将所有函数内的名字全放到名称空间内了。该方法虽然好用 但是在某些情况下回造成资源的浪费

4.3 Data types supported by the template layer (special case)

Summarize:

1 All python data types can be passed to the front-end page

2 Note that when passing a function, it will automatically add parentheses to call the front end to display the return value after the function call
(ps: the front end does not support passing extra parameters to the function when calling the function)
If you want to get the back end in the front end The specific element in a container type passed,
then you can get the specific element through the period character
1. Index
2. Key

​ 4.3.1 The incoming object is :

  <p>{
     
     { obj }}</p> #显示对象的内存地址

#此时能在浏览器页面显示对象的属性值
<p>{
     
     { obj.get_self }}</p>
<p>{
     
     { obj.get_cls }}</p>
<p>{
     
     { obj.get_static }}</p>

​ 4.3.2 What is passed in is the function

​ Note: Django's template syntax does not support passing parameters to functions!!!

1传函数名,会自动加括号调用该函数,前端展示的是函数调用之后的返回值:{
     
     { index }};
2django的模板语法 不支持给函数传参!!!
3无返回值则页面显示None

​ 4.3.3 Incoming time: time can be displayed

from datetime import 
datetimectime = datetime.now()

# 默认显示的时间格式
Sept. 18, 2019, 7:53 p.m.# 可通过|date过滤器显示不同格式的时间

4.4 Filters :|

lengthdefaultfilesizeformattruncatewords


<h1>模板语法之标签:内部原理(会将|前面的当做第一个参数传入标签中)</h1>
# length
<p>{
     
     { l|length }}</p>
<p>{
     
     { n|length }}</p>
# default
<p>{
     
     { ss|default:'当|左边的变量的值为空就会返回|右边的值' }}  default跟你后端get方法很想</p>
<p>{
     
     { ss|default:'' }} default必须要有两个参数</p>
# filesizeformat 会将文件大小转换成MB
<p>{
     
     { file_size|filesizeformat }}</p>
# truncatewords:6按照空格切分
<p>{
     
     { info|truncatewords:3 }} #就是按空格截取  三个点不算</p>
# 按照字符切分三个点也算,也就意味着后面的数字起码得大于3
<p>{
     
     { info1|truncatechars:6 }}</p>
# |date:2019-09-18
<p>{
     
     { ctime|date:'Y-m-d' }} 只需要掌握年月日就可以了</p>#结果2019-09-18
# |add 拼接数字或字符串
<p>{
     
     { s|add:'哈哈哈' }}</p>
# |slice 列表切片
<p>{
     
     { l|slice:'0:3' }}</p># [1, 2, 3]
<p>{
     
     { l|slice:'0:5:2' }}</p># [1, 3, 5]

4.5 Template syntax to prevent script attacks

When displayed by the browser, the syntax is not recognized! The following content shows you how to demonstrate the grammar!

后端代码
yyy = '<script>alert(123)</script>'

前端代码
<p>{
     
     { yyy }}</p>
# 在浏览器显示的时候,不识别语法!

Unescape the front and back ends : Unescape the front and back ends: |safe, display the meaning of the label

​ Front-end escape: |safe

<p>{
    
    { xxx|safe }}</p>

​ Backend escape: need to import module

from django.utils.safestring import mark_safe
zzz = mark_safe('<h1>阿萨德搜啊第三款垃圾袋</h1>')

in conclusion:

前后端取消转义:
    也及意味着前端的html代码 并不单单只能在html文件中书写
    你也可以在后端先生成html代码 然后直接传递给前端(******)

Replenish:

1. Three binding methods for classes

class Demo(object):
    def get_self(self):
        return '绑定给对象的方法'
    @classmethod
    def get_cls(cls):
        return '绑定给类的方法'
    @staticmethod
    def get_static():
        return '我是静态方法  其实就是函数'
    
    {#<p>{
     
     { n }}</p>#}  <p>{
     
     { n }}</p>

2. Annotation of the html front-end page

{##}

Label: {%%} logically related

1for loop

# 将l列表中的每个值取出来
{% for foo in l %}   
<p>{
     
     { foo }}</p>
{% endfor %}

{% for foo in l %}
    <p>{
     
     { forloop }}</p>
{% endfor %}

2if judgment

{% if xxx %}
if条件成立
{% elif xxx %}
elif条件成立
{% else %}
上面两者都不成立
{% endif %}

3.for loop + nested if judgment

{% for %}
{
     
     { forloop }}
{
     
     { forloop.first }}
{
     
     { forloop.last }}
{
     
     { forloop.counter0 }}
{
     
     { forloop.counter }}  数据库中的数据它的主键值 不可能是依次递增的  有可能被你删除了几条数据  主键值就不再连续的了
{% empty %}
当for循环对象不能被for循环的时候会走empty逻辑
{% endfor %}

for loop if nested case


# 嵌套if判断
{% for foo in l %}
    {% if forloop.first %}
        {% elif forloop.last %}
        {% else %}
    {% endif %}
    {% empty %}
    <p>当for循环的对象为空的时候 会走empty</p>
{% endfor %}

I didn't understand it at first: read it

loop determines whether the current loop is the first or the last time

4with: django value uses dot syntax to retrieve the value in the list

l = [1,2,3,4,5,6,[12,3,4,{'name':'heiheihei'}]]
<p>
    django模板语法在取值的时候 统一使用句点符(大白话就是 点号   .)
    {% with l.6.3.name as ttt %}  可以给一个比较复杂的取值操作取一个别名 之后在with语句中 就可以使用该别名
        {
     
     { ttt }}
        {
     
     { l.6.3.name }}
    {% endwith %}
</p>

5for loop + dictionary value

{#d = {"name":'jason','password':123}#}
{% for foo in d.keys %}
    <p>{
     
     { foo }}</p>
{% endfor %}
{% for foo in d.values %}
    <p>{
     
     { foo }}</p>
{% endfor %}
{% for foo in d.items %}
    <p>{
     
     { foo }}</p>
{% endfor %}

3. Custom filters, tags, inclusion_tag

1. Customize the fixed three-step strategy:

1.必须在你的应用下新建一个名为templatetags文件夹
2.在该文件夹内新建一个任意名称的py文件
3.在该py文件中固定先写下面两句代码
from  django import template
register = template.Library()

2. Custom Filters

# mytag页面
@register.filter(name='baby')
def mysum(a,b):
    retunr a + b
# html页面
{% load mytag %}
{
     
     { 123|baby:1}} #结果为124

2. Custom label (support multiple values)

grammar:

# mytag页面
@register.simple_tag(name='jason')
def myplus(a,b,c,d,e):
    return None
# html页面
{% load mytag %}
{% jason 1 2 3 year=2 %}

case

@register.simple_tag(name='jason')
def xxx(a,b,c,year):
    return '%s?%s|%s{%s'%(a,b,c,year)

{% load mytag %}
{% jason 1 2 3 year=2 %}
#结果:1?2|3{2

3. Customize inclusion_tag

When there is a part of html code on your page that needs to be used frequently in various places and needs to pass parameters to be rendered, then you can make the html code part into an inclusion_tag so that it can be used on any page

Syntax: @register.inclusion_tag('html page name')

# mytag页面
@register.inclusion_tag('bigplus.html')
def mytag(n):
    ...
    # return {'l':l} # 两种return的方式!
    return locals()

# 访问的页面
{% load mytag %}
{% bigplus 5 %}
# bigplus.html
<ul>
    {% for foo in l %}
        <li>{
     
     { foo }}</li>
    {% endfor %}
</ul>

Note the way the two return

# 示例
@register.inclusion_tag('bigplus.html')
def bigplus(n):
    l = []
    for i in range(n):
        l.append('第%s项'%i)
    return {'l':l}
    # 或者
    return locals()

{% load mytag %}
{% bigplus 5 %}

<br>
{% bigplus 10 %}

Fourth, template inheritance (more used than template import)

Use block syntax to rivet the template, then other pages inherit the process, and then write your own content in the rivet

foreword
当多个页面整体的样式都大差不差的情况下 可以设置一个模板文件
在该模板文件中 使用block块划分多个预期
之后子版在使用模板的时候 可以通过block块的名字 来选定到底需要修改哪一部分区域

Templates should generally have at least three areas that can be modified

{% block css %}
子页面自己的css代码
{% endblock %}

{% block content %}
子页面自己的html代码
{% endblock %}

{% block js %}
子页面自己的js代码
{% endblock %}

inherit

Inherit the master on other pages via the "extend" keyword.

All inherited! !

{% extends 'home.html' %}
#将home页面 继承过来#

Revise

{% block css %}
子页面自己的css代码
{% endblock %}

{% block content %}
子页面自己的html代码
{% endblock %}

{% block js %}
子页面自己的js代码
{% endblock %}

Contents of calling template repeatedly: { { block.super }}

{
     
     { block.super }}

    {% include 'beautiful.html' %}

    <form action="">
        <p>username:<input type="text" class="form-control"></p>
        <p>password:<input type="text" class="form-control"></p>
        <input type="submit" class="btn btn-danger">
    </form>
    {
     
     { block.super }}
    {
     
     { block.super }}
    {
     
     { block.super }}

5. Template import

{% include 'the name of the html file you want to import' %}

This usage rate is not as high as the one above, and it is also imported. It can only be used directly in a good location in the sub-html page

Add, delete, modify and check for single table of model layer (important)

Add, delete, modify and search for single table operations

Key parameters:

1. auto_now ; operation data, refresh operation time

  1. auto_now_add ; record creation time
    link database: direct two commands

Create a new test.py file: In this way, you don't need to use the web page test, you can test it directly in the py file!

# test内的内容
import os
import sys

if __name__ == "__main__":
    # django在启动的时候 就会往全局的大字典中设置一个键值对  值是暴露给用户的配置文件的路径字符串
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day54.settings")

    import django
    django.setup()
    from app01 import models
    models.Book.objects.all()

increase

Two ways to increase:

# 增
# 方式1: create
book_obj  = models.Book.objects.create(title='三国',price=19.99,create_time='2019-11-11')
print(book_obj.title)

# 方式2:对象点save()方法。save方法效率极低!
from datetime import datetime
ctime = datetime.now()
book_obj = models.Book(title='jpm',price=96.66,create_time=ctime)
book_obj.save()

check

PK replaces the primary key value: pk will automatically find the primary key field of the current data

# 三张查找方法
print(models.Book.objects.all())
# 查询出来的结果是queryset对象

#<QuerySet [<Book: 三国演义>, <Book: jmp>, <Book: 西游记P>, <Book: 水浒传>, <Book: 三国演义>, <Book: 围城>, <Book: 床下有人>, <Book: 床下有人2>, <Book: 床下有人3>, <Book:jpm2>]>#


print(models.Book.objects.get(id=1)) # 不要轻易使用get
print(models.Book.objects.filter(pk=1))# pk会自动查找到当前数据的主键字段

change

# 1.update
models.Book.objects.filter(pk=1).update(title='三国演义')
# 2.对象.save()
book_obj = models.Book.objects.get(pk=1)
book_obj.price = 666.66
book_obj.save()

delete

models.Book.objects.filter(pk=2).delete()

The magic double glide method

PK: Automatically find out the primary key.

value (value1, value2). There is no "ID" taken out!

(12) Not much is used.

   # < 1 > all(): 查询所有结果

    # < 2 > filter(**kwargs): 它包含了与所给筛选条件相匹配的对象
    # < 3 > get(**kwargs): 返回与所给筛选条件相匹配的对象,返回结果有且只有一个,如果符合筛选条件的对象超过一个或者没有都会抛出错误。(源码就去搂一眼~诠释为何只能是一个对象)
    # < 4 > exclude(**kwargs): 它包含了与所给筛选条件不匹配的对象
    # print(models.Book.objects.exclude(pk=1))  # 只要pk不是1的数据全部查询出来


    # < 5 > order_by(*field): 对查询结果排序('-id') / ('price')
    # print(models.Book.objects.order_by('price'))  # 默认是升序
    # print(models.Book.objects.order_by('-price'))  # 加负号就是降序


    # < 6 > reverse(): 对查询结果反向排序 >> > 前面要先有排序才能反向
    # print(models.Book.objects.order_by('price').reverse())

    # < 7 > count(): 返回数据库中匹配查询(QuerySet)
    # print(models.Book.objects.count())  # 对查询出来的结果进行一个计数


    # 的对象数量。
    # < 8 > first(): 返回第一条记录
    # print(models.Book.objects.filter(pk=1).first())
    # < 9 > last(): 返回最后一条记录
    # print(models.Book.objects.all())
    # print(models.Book.objects.all().last())
    
    
    #10是最没用用的
    # < 10 > exists(): 如果QuerySet包含数据,就返回True,否则返回False
    # print(models.Book.objects.filter(pk=1000))
    # print(models.Book.objects.filter(pk=1000).exists())


    # < 11 > values(*field): 返回一个ValueQuerySet——一个特殊的QuerySet,运行后得到的并不是一系列model的实例化对象,而是一个可迭代的字典序列
    # print(models.Book.objects.values('title','price'))  # 得到的结果是列表套字典



    # < 12 > values_list(*field): 它与values()
    # print(models.Book.objects.values_list('title','price'))  # 得到的结果是列表套元组


    # 非常相似,它返回的是一个元组序列,values返回的是一个字典序列
    # < 13 > distinct(): 从返回结果中剔除重复纪录
    """
    去重的前提是 一定要有完全重复的数据 才能去重
    """
    # print(models.Book.objects.filter(title='三国演义').distinct())
    # print(models.Book.objects.values('title','price','create_time').distinct())

Reprinted in: https://www.cnblogs.com/ZDQ1/p/11565484.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326568911&siteId=291194637