emptygif
语言: 简体中文

所有语言和框架

Django Python

一个用于 Django 的 Python 代码片段,以空 GIF 响应对 /pixel.gif 的请求,GIF 在启动时只解码一次。

Django Python

安装
pip install django
文件
views.py
运行
python manage.py runserver 8080
# pixels/views.py
import base64

from django.http import HttpResponse
from django.views.decorators.http import require_GET

GIF = base64.b64decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")


@require_GET
def pixel(request):
    return HttpResponse(GIF, content_type="image/gif", headers={"Cache-Control": "no-store"})


# pixels/urls.py  (include("pixels.urls") from the project's urls.py)
from django.urls import path

from . import views

urlpatterns = [
    # Django URL patterns are written without the leading slash.
    path("/pixel.gif".lstrip("/"), views.pixel, name="pixel"),
]

我们运行了此代码片段,并比对了它返回的字节。

验证

curl -sI http://localhost:8080/pixel.gif

官方网站: Django

更多 Python 示例