[ Django辅助层utils ]
发表评论
78 views
分类目录归档:Django-Dev
class TemplateView(TemplateResponseMixin, ContextMixin, View):
"""
Render a template. Pass keyword arguments from the URLconf to the context.
"""
def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
pre_init
django.db.models.signals.pre_init
每当你实例化一个 Django 模型时,这个信号都会在模型的 __init__() 方法的开头发出。
用此信号发送的参数:
sender
刚刚创建了一个实例的模型类。
args
传递给 __init__() 的位置参数列表。
kwargs
传递给 __init__() 的关键字参数字典。
post_init
django.db.models.signals.post_init
和 pre_init 一样,但这个是在 __init__() # 安装channels
python -m pip install -U channels["daphne"]
# 插件redis用于实现组之间群发消息的
python -m pip install channels_redis
# 1. 添加app
INSTALLED_APPS = (
"daphne",
"django.contrib.auth",
"django.contrib.contenttypes",
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.刚开始会获取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