如何在开发过程中为Django提供CSS? [英] How do I serve CSS to Django in development?

查看:108
本文介绍了如何在开发过程中为Django提供CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在通过文档,这对我来说没有意义。我运行collectstatic,我在我的应用程序和我的项目目录中设置/静态/目录,我添加了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',
#取消注释下一行以启用admin:
' django.contrib.admin',
#取消注释下一行以启用管理员文档:
#'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 =stylesheethref ={{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

#取消注释下面两行以启用admin:
from django .contrib import admin
admin.autodiscover()

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

#取消注释下面的admin / doc行以启用管理员文档:
#(r'^ admin / doc /',include('django .contrib.admindocs.urls'))

#取消注释下一行以启用管理员:
(r'^ admin /',include(admin.site.urls)),
('^ show_project / $',show_project),


如果settings.DEBUG:
urlpatterns + = patterns('',
url (r'^ media /(?P< path>。*)$','django.views.static.serve',{'document_root':settings.MEDIA_ROO T,'show_indexes':True}),
url(r'^ static /(?P< path>。*)$','django.views.static.serve',{'document_root' 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上运行)从来没有收到生产中静态资产的请求。



CONTEXT PROCESSOR



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

  TEMPLATE_CONTEXT_PROCESSORS =(
#其他上下文处理程序....
'django.core.context_processors.static',
#其他上下文处理程序....

开发中的SERVER STATIC资产



Django在生产中没有获得静态资产的请求,但是在开发中,我们只是让Django提供静态内容。我们在 urls.py 中使用 staticfiles_urlpatterns 来告诉Django提供 static / *

  from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
#....你的url模式在这里...
urlpatterns + = staticfiles_urlpatterns()


I've been all through the documentation, and it just doesn't make sense to me. I ran collectstatic, I set up /static/ directories in both my app and my project directories, I added STATIC_URL and STATIC_ROOT to my settings.py file (but I have no idea how to know if they're set correctly) and {{ STATIC_URL }} still isn't rendering out to anything. It all seems like a heck of a lot of overkill just to connect html to css.

I think I'm lost in the details; could anyone supply a high-level breakdown of this static files idea? I'm afraid I may have mixed instructions for both production and development setups.

MORE: Here's the relevant bit from my settings.py file:

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/',
)

And this is the code I'm trying to use in my template:

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

OK. I made the changes everybody recommended. Here's my new 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()

Am I missing something? Usually my problems turn out to be something really basic that CS pros take for granted but I miss.

解决方案

Here's how mine is setup. It sounds like you might be missing the static context processor?

STATIC_ROOT and STATIC_URL

In the settings.py used in development:

STATIC_ROOT = ''
STATIC_URL = '/static/'

And the settings.py used on my production server:

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

So, all the static files are located in static/. On the production server, all these files in static/ are collected to /home/USER/public_html/static.MYDOMAIN.com/ where they are served by a different web server (nginx in my case) and not Django. In other words, my django application (running on Apache) never even receives requests for static assets in production.

CONTEXT PROCESSOR

In order for templates to have the STATIC_URL variable available to them, you need to use the django.core.context_processors.static context processor, also defined in settings.py:

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

SERVER STATIC ASSETS IN DEVELOPMENT

Django doesn't get requests for static assets in production, however, in development we just let Django serve our static content. We use staticfiles_urlpatterns in urls.py to tell Django to serve requests for static/*.

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

这篇关于如何在开发过程中为Django提供CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆