Skip to content

Latest commit

 

History

History
84 lines (67 loc) · 1.83 KB

views.md

File metadata and controls

84 lines (67 loc) · 1.83 KB
  • serve 返回静态文件
from django.conf import settings
from django.urls import re_path
from django.views.static import serve

# ... the rest of your URLconf goes here ...

if settings.DEBUG:
    urlpatterns += [
        re_path(
            r"^media/(?P<path>.*)$",
            serve,
            {
                "document_root": settings.MEDIA_ROOT,
            },
        ),
    ]
urlpatterns += [
    re_path("static/(?P<path>.*)$",
            serve,
            {
                "document_root": settings.STATIC_ROOT,
                "show_indexes": True}),
]

URL

./urls.md
request.FILES['file']
  • 编写自己的Middleware
def simple_middleware(get_response):
    # One-time configuration and initialization.

    def middleware(request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.

        response = get_response(request)

        # Code to be executed for each request/response after
        # the view is called.

        return response

    return middleware

内置的middleware

  • CommonMiddleware
    • 禁止Disallowed_user_agents访问
    • 自动append_slash和prepend_www
    • 设置Content-Length

获取请求的文件

request.FILES['file']

csrf

from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def view(request):
    return HttpResponse('csrf')