- 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}),
]
./urls.md
-
URLconfs 解析url, 反编译url
-
Requests and Response
FILE uploads 文件上传
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
- CommonMiddleware
- 禁止Disallowed_user_agents访问
- 自动append_slash和prepend_www
- 设置Content-Length
request.FILES['file']
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def view(request):
return HttpResponse('csrf')