[ flask序列化器 ]
发表评论
133 views
分类目录归档:backend
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_pfrom functools import wraps
def ex(get_response):
@wraps(get_response)
def inner(request):
try:
print('wraps-request')
response = get_response(request)
except 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 class M(type):
def __new__(cls)
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是无关
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 通过使用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, passwpip install django-haystack
# 使用es请安装
pip install "django-haystack[elasticsearch]"
本文展示用whoosh检索
import os
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.pa