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 範例