0

我在处理 django 静态文件时遇到问题。

我对如何使用 MEDIA_ROOT、STATIC_ROOT、MEDIA_URL 和 STATIC_URL 感到困惑

我有这样的文件结构(对不起,我不知道如何正确缩进:S):

static/
    css/
    img/
    js/

例如,如果我的 css 目录中有一个 .css 文件,我该如何访问它?

4

1 回答 1

0

MEDIA_ROOT 和 MEDIA_URL 已弃用。使用 STATIC_ROOT 和 STATIC_URL。

STATIC_ROOT = "Absolute path to your static directory" (Example: /home/your_app/static)
STATIC_URL = "/static/" (Could be anything you would like to use)

当您想提供静态内容时,请传递 context_instance 并在您的模板中使用,

{{STATIC_URL}}/file_path_from_static_directory/

编辑:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns += staticfiles_urlpatterns()
于 2012-11-21T20:21:21.980 回答