Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Django - 安装 WhiteNoise


WhiteNoise

Django 没有内置的静态文件服务解决方案,至少在生产环境中 DEBUG 必须为 False 时没有。

我们必须使用第三方解决方案来实现这一点。

在本教程中,我们将使用 WhiteNoise,这是一个用于服务静态文件的 Python 库。


安装 WhiteNoise

要在虚拟环境中安装 WhiteNoise,请键入以下命令

pip install whitenoise

结果应该是这样的

Collecting whitenoise
  Downloading whitenoise-6.2.0-py3-none-any.whl (19 kB)
Installing collected packages: whitenoise
Successfully installed whitenoise-6.2.0
WARNING: You are using pip version 20.2.3; however, version 22.3.1 is available.
You should consider upgrading via the 'c:\users\Your Name\myworld\scripts\python.exe -m pip install --upgrade pip' command.

修改设置

为了让 Django 知道你要运行 WhitNoise,你必须在 settings.py 文件中的 MIDDLEWARE 列表中指定它。

my_tennis_club/my_tennis_club/settings.py:

.
.
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
].
.

收集静态文件

在你可以从 上一章 中的示例中提供静态文件之前,你必须执行一个操作。你必须收集所有静态文件并将它们放到一个指定的文件夹中。你将在 下一章 中学习如何做到这一点。


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.