python - 如何在开发中为 Django 提供 CSS?

标签 python django

我已经阅读了所有文档,但对我来说这没有任何意义。我运行了 collectstatic,我在我的应用程序和我的项目目录中设置了/static/目录,我将 STATIC_URL 和 STATIC_ROOT 添加到我的 settings.py 文件中(但我不知道如何知道它们是否设置正确)和 {{ STATIC_URL }} 仍然没有呈现出任何东西。只是将 html 连接到 css 似乎有点矫枉过正。

我想我迷失了细节;任何人都可以提供这个静态文件想法的高级分割吗?恐怕我可能对生产和开发设置有不同的说明。

更多:这是我的 settings.py 文件中的相关部分:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'django.contrib.staticfiles',
    'dashboard.base',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
)

STATIC_ROOT = ''
STATIC_URL = '/static/'


STATICFILES_DIRS = (
    'C:/Users/Sean/Desktop/Work Items/dashboard/base/static/',
)

这是我试图在我的模板中使用的代码:

<link rel="stylesheet" href="{{ STATIC_URL }}css/960.css" />

好的。我做了大家推荐的改变。这是我的新 urls.py:

from django.conf.urls.defaults import *
from base.views import show_project
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Example:
# (r'^dashboard/', include('dashboard.foo.urls')),

# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
('^show_project/$', show_project),
)

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True }),
        url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True }))

urlpatterns += staticfiles_urlpatterns()

我错过了什么吗?通常我的问题都是非常基本的问题,CS 专业人士认为这是理所当然的,但我想念。

最佳答案

这是我的设置方式。听起来您可能缺少静态上下文处理器?

STATIC_ROOT 和 STATIC_URL

在开发中使用的settings.py中:

STATIC_ROOT = ''
STATIC_URL = '/static/'

以及在我的生产服务器上使用的 settings.py:

STATIC_URL = '//static.MYDOMAIN.com/'
STATIC_ROOT = '/home/USER/public_html/static.MYDOMAIN.com/'

因此,所有静态文件都位于static/ 中。在生产服务器上,static/ 中的所有这些文件都被收集到 /home/USER/public_html/static.MYDOMAIN.com/ 中,它们由不同的 Web 服务器提供服务(在我的例子中是 nginx)而不是 Django。换句话说,我的 Django 应用程序(在 Apache 上运行)甚至从未在生产环境中收到对静态 Assets 的请求。

上下文处理器

为了使模板能够使用 STATIC_URL 变量,您需要使用 django.core.context_processors.static 上下文处理器,也在 中定义>settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
    # other context processors....
    'django.core.context_processors.static',
    # other context processors....
)

开发中的服务器静态 Assets

Django 在生产环境中不会收到对静态 Assets 的请求,但是在开发环境中,我们只是让 Django 为我们的静态内容提供服务。我们在 urls.py 中使用 staticfiles_urlpatterns 来告诉 Django 为 static/* 的请求提供服务。

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# .... your url patterns are here ...
urlpatterns += staticfiles_urlpatterns()

关于python - 如何在开发中为 Django 提供 CSS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7620307/

相关文章:

python - 当 Django models 字段为空时,将值设置为默认值

python - 在 Django 表单中动态将 CharField 转换为 ChoiceField?

python - IO错误 : [Errno 13] Permission denied

python - python/numpy中的数据压缩

python - Jython 有 GIL 吗?

django - select_related 与反向外键

python - 如何使用 django-paypal 获取付款通知

python - 如何在 Google Colab 中处理来自 Google Drive 的视频

django - 如何使用 Pytest 在 Django 中测试缓存存储(Redis)?

django - 没有名为 backends.simple.urls 的模块