使用静态文件与django虚拟服务器 [英] Using static files with the django virtual server

查看:176
本文介绍了使用静态文件与django虚拟服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题就像这样的问题之前,我已经阅读他们的所有,并试图理解有关的主题的官方django文档,但我仍然努力在虚拟服务器上使用静态文件。更具体地说,我只是想获得我的模板 Base.html ,使用 base.css。 p>

我的文件夹结构如下所示:

  manage.py 
static / CSS / base.css
VergeGreenITEvent_Website / settings.py
VergeGreenITEvent_Website / views.py ect
VergeGreenITEvent_Website / Webpage_Templates / Base.html

(目前没有应用程序文件夹,因为我在下面的django书学习,没有得到这一点!)



完整的settings.py可以在这里查看: http:// pastebin.com/JB3mKRcJ



在我的Base.html模板中,我现在有代码:

 < head> 
< title> Verge Green IT< / title>
{%load static%}

< link rel =stylesheethref ={%staticCSS / base.css%}type =text / css/ >
< / head>

尚未应用CSS。你能帮我弄清楚我做错了吗?我会非常感激。



我使用的是最新版本的Django。 (1.4)






urls.py:

 来自django.conf.urls导入模式,include,url 
来自django.conf导入设置
来自django.contrib.staticfiles.urls import staticfiles_urlpatterns
import views

urlpatterns = patterns('',

url(r'^ $',views.Home),



if settings.DEBUG:
urlpatterns + = staticfiles_urlpatterns()#this提供静态文件和媒体文件。
#in case media is not served correctly
urlpatterns + = patterns('',
url(r'^ media /(?P< path> *)$','django。 views.static.serve',{
'document_root':settings.MEDIA_ROOT,
}),

解决方案

如果你使用Django 1.4,那么我将使用静态标签:

  {%load static%} 

< link rel =stylesheethref ={%staticCSS / base.css%}type = text / css/>

您可能还需要在您的url中,开发文件通过django提供

 如果settings.DEBUG:
urlpatterns + = staticfiles_urlpatterns()#this提供静态文件和媒体文件。
#in case media is not served correctly
urlpatterns + = patterns('',
url(r'^ media /(?P< path> *)$','django。 views.static.serve',{
'document_root':settings.MEDIA_ROOT,
}),


在你的设置中,通常的做法是避免硬编码位置,这里有一个例子(你的设置中有静态文件发现器的方式吗?

  PROJECT_ROOT = path.dirname(path.abspath(__ file__))#gets目录设置在

中STATIC_ROOT = path.join(PROJECT_ROOT, static-root')
#此文件夹用于收集生产中的静态文件,不在开发中使用

STATIC_URL =/ static /

STATICFILES_DIRS = (
('',path.join(PROJECT_ROOT,'static')),#store site-specific media here。


#在
#各种位置中查找静态文件。
STATICFILES_FINDERS =(
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder ',


TEMPLATE_CONTEXT_PROCESSORS =(
#其他处理器...
django.core.context_processors.static,


Questions exactly like this have been asked before, i've read them all and tried to make sense of the official django documentation on the subject but i'm still struggling to use static files on the virtual server. More specifically i'm just trying to get my template, Base.html, to use base.css.

My folder structure looks like this:

manage.py
static/CSS/base.css
VergeGreenITEvent_Website/settings.py
VergeGreenITEvent_Website/views.py ect
VergeGreenITEvent_Website/Webpage_Templates/Base.html

(no app folder at the moment, as I was following "The django book" to learn and hadn't gotten to that bit!)

The complete settings.py can be viewed here: http://pastebin.com/JB3mKRcJ

In my Base.html template I now have the code:

<head>
    <title>Verge Green IT</title>
    {% load static %}

    <link rel="stylesheet" href="{% static "CSS/base.css" %}" type="text/css" />
</head>

CSS still isn't being applied. Could you help me figure out what i'm doing wrong? I'd be incredibly grateful.

I'm using the latest version of Django. (1.4)


urls.py :

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
import views

urlpatterns = patterns('',

    url(r'^$', views.Home),

)

if settings.DEBUG:
    urlpatterns += staticfiles_urlpatterns() #this serves static files and media files.
    #in case media is not served correctly
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
            }),
    )

解决方案

If you're using Django 1.4 then I would use the static tag:

{% load static %}

<link rel="stylesheet" href="{% static "CSS/base.css" %}" type="text/css" />

You probably need this in your urls too, in development files are served via django

if settings.DEBUG:
    urlpatterns += staticfiles_urlpatterns() #this serves static files and media files.
    #in case media is not served correctly
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
            }),
    )

Also in your settings it's common practice to avoid hardcoding locations, here's an example (by the way do you have the static filefinders in your settings?

PROJECT_ROOT = path.dirname(path.abspath(__file__)) #gets directory settings is in

STATIC_ROOT = path.join(PROJECT_ROOT,'static-root')
# this folder is used to collect static files in production. not used in development

STATIC_URL =  "/static/"

STATICFILES_DIRS = (
    ('', path.join(PROJECT_ROOT,'static')), #store site-specific media here.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

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

这篇关于使用静态文件与django虚拟服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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