django扩展知识

  • 虚拟环境
  • django版本区别
  • MTV与MVC模型
  • importlib

虚拟环境(了解)

在正常开发中 我们会给每一个项目配备一个该项目独有的解释器环境
该环境内只有该项目用到的模块 用不到一概不装

"""
虚拟环境
	你每创建一个虚拟环境就类似于重新下载了一个纯净的python解释器
	但是虚拟环境不要创建太多,是需要消耗硬盘空间的

扩展:
	开发当中我们会给每一个项目配备一个requirements.txt文件
	里面书写了该项目所有的模块即版本
	你只需要直接输入一条命令即可一键安装所有模块即版本
"""

django版本区别


"""1.路由匹配不同"""
django1.X路由层使用的是url方法,而在django2.Xhe3.X版本中路由层使用的是path方法
url()第一个参数支持正则
path()第一个参数是不支持正则的 写什么就匹配什么

如果你习惯使用正则匹配那么也给你提供了其它方法

from django.urls import path, re_path ## 第一种re_path
from django.conf.urls import url ## 第二种导入1.X的url

re_path(r'^index/',index),
url(r'^login/',login)

2.X和3.X里面的re_path就等价于1.X里面的url


"""2.path内部的五种转换器 """

path('index/<int:id>/',index) # 将第二个路由里面的内容先转成整型然后以关键字的形式传递给后面的视图函数

def index(request,id):
    print(id,type(id))
    return HttpResponse('index')

str,匹配除了路径分隔符(/)之外的非空字符串,这是默认的形式
int,匹配正整数,包含0。
slug,匹配字母、数字以及横杠、下划线组成的字符串。
uuid,匹配格式化的uuid,如 075194d3-6885-417e-a8a8-6c931e272f00。
path,匹配任何非空字符串,包含了路径分隔符(/)(不能用?)

除了有默认的五个转换器之外 还支持自定义转换器(了解)

## app01 path_converts.py
class MonthConverter:
    regex='d{2}' # 属性名必须为regex

    def to_python(self, value):
        return int(value)

    def to_url(self, value):
        return value # 匹配的regex是两个数字,返回的结果也必须是两个数字

## app01 urls.py
from django.urls import path,register_converter
from app01.path_converts import MonthConverter

# 先注册转换器
register_converter(MonthConverter,'mon')

from app01 import views

urlpatterns = [
    path('articles/<int:year>/<mon:month>/<slug:other>/', views.article_detail, name='aaa'),
]

## app01 views.py
from django.shortcuts import render,HttpResponse,reverse

def article_detail(request,year,month,other):
    print(year,type(year))
    print(month,type(month))
    print(other,type(other))
    print(reverse('xxx',args=(1988,12,'hello'))) # 反向解析结果/articles/1988/12/hello/
    return HttpResponse('xxxx')


"""3.orm不同之处"""
模型层里面1.X外键默认都是级联更新删除的
但是到了2.X和3.X中需要你自己手动配置参数

models.ForeignKey(to='Publish',on_delete=models.CASCADE...)

MTV与MVC模型

# MTV:Django号称是MTV模型
M:models
T:templates
V:views
# MVC:其实django本质也是MVC
M:models
V:views
C:controller
# vue框架:MVVM模型

importlib

可以通过字符串来导入另外一个模块


# lib/demo.py
name = 'egon'
age = 12

def fun(name, age):
    print(name, age)


# test.py
import importlib

path = 'lib.demo' # 想要获取的模块路径,最多只能点到py文件名
modobj = importlib.import_module(path) # 调用import_module()方法,获取模块对象
print(modobj)
print(modobj.name)  # modobj可以调用这个demo模块下的所有属性和方法
modobj.fun('xxx', 18)

# 执行输出
<module 'lib.demo' from 'D:\pycharm\DjangoProj\djangotest2\app01\lib\demo.py'>
egon
xxx 18

设计思想

# start.py
import notify

notify.send_all('快下课了')


# settings.py
# 可以在这很方便的扩展功能
NOTIFY_LIST = [
    'notify.email.Email',
    'notify.qq.QQ',
    'notify.wechat.Wechat',
    # 'notify.msg.Msg',
]


# notify/__init__.py
import settings
import importlib

def send_all(content):
    for path_str in settings.NOTIFY_LIST: 
        module_path,class_name = path_str.rsplit('.',maxsplit=1)
        # module_path = 'notify.email'  class_name = 'Email'
        # 1 利用字符串导入模块
        module = importlib.import_module(module_path)  # from notify import email
        # 2 利用反射获取类名
        cls = getattr(module,class_name)  # Email、QQ、Wechat
        # 3 生成类的对象
        obj = cls()
        # 4 利用鸭子类型直接调用send方法
        obj.send(content)

# notify/wechat.py
class Wechat(object):
    def __init__(self):
        pass  # 发送微信需要做的前期准备工作

    def send(self,content):
        print('微信通知:%s'%content)

# notify/qq.py
class QQ(object):
    def __init__(self):
        pass  # 发送qq需要做的前期准备工作

    def send(self, content):
        print('qq通知:%s' % content)

# notify/email.py
class Email(object):
    def __init__(self):
        pass  # 发送邮箱需要做的前期准备工作

    def send(self, content):
        print('邮箱通知:%s' % content)

# notify/msg.py
class Msg(object):
    def __init__(self):
        pass

    def send(self,content):
        print('短信通知:%s'%content)
原文地址:https://www.cnblogs.com/chenwenyin/p/12974348.html