分类目录归档:Python-Dev

[ Django文件下载 ]


views.py

from django.shortcuts import render
from django.http import StreamingHttpResponse
# Create your views here.

def index(request):
    return render(request, 'index.html')

def upload(request):
    res = StreamingHttpResponse(open('/home/ouzijun/ouzijun/Django_postgresSQL/django_p

< Read more >

[ django[middleware]中间件源码解析 ]


源码执行顺序

1. 通过嵌套装饰器wraps实现, 调用__call__魔术方法, 先执行request [321]里到外, 再执行response [123]外到里

from functools import wraps

def ex(get_response):
    @wraps(get_response)
    def inner(request):
        try:
            print('wraps-request')
            response = get_response(request)
        except 

< Read more >

[ Python魔法方法[属性]2 ]


1.getattr魔法方法 当读取这个属性不存在的时候, 你希望它做什么

class A:
    def __getattr__(self, name):
        print('getting')
        raise AttributeError

o = A()
print(o.test)

2.getattribute魔法方法 只要尝试读取属性都会被调用

class A:
    def __init__(self):
        self.data = 'abc'
        self.counter = 0

    def 

< Read more >

[ Python魔法方法[1] ]


1.获取new, init魔法方法 - 先使用__new__方法创建完成对象后, 再执行__init__方法

class A:
    def __new__(cls, x):
        print('__new__')
        return super().__new__(cls)

    def __init__(self, x):
        self.x = x
        print('__init__')
 o = A(1)

2.del魔法方法 - 在实例被释放的时候触发, 不可控 - 与python里的 del o是无关

< Read more >

[ Django中间件解析 ]


源码解析

1.刚开始会获取settings.py里配置的中间件 reversed反向

for middleware_path in reversed(settings.MIDDLEWARE):

2.process_view 添加到<视图中间件>列表前面 3.process_template_response 添加到<模板中间件>列表后面 4.process_exception 添加到<异常中间>件列表后面

if hasattr(mw_instance, "process_view"):
    # 1. 中间件存在方法proce

< Read more >

[ celery-django基本配置 ]


Celery官方文档

@app.task()

 通过使用app.task()装饰器,您可以轻松地从任何可调用的对象创建任务: 
from .models import User

@app.task
def create_user(username, password):
    User.objects.create(username=username, password=password)

 还可以为任务设置许多选项,这些选项可以指定为装饰器的参数:
 @app.task(serializer='json')
def create_user(username, passw

< Read more >

[ 密钥注意事项 ]


http://127.0.0.1:8000/api/token/ #账号获取密钥

  • 如果access过期了refresh没过期可以刷新,如果都过期了需要重新发送用户名密码获取才能使用

http://127.0.0.1:8000/api/token/verify/ #密钥获取数据

  • 密钥令牌refresh(刷新),access(令牌)都可以获取数据

http://127.0.0.1:8000/api/token/refresh/ #刷新密钥

  • 如果access过期了refresh没过期可以刷新,如果都过期了需要重新发送用户名密码获取才能使用

< Read more >