简体   繁体   English

Django 1.4静态文件CSS未应用

[英]Django 1.4 static files css not applied

I have spent the last few days figuring out how to include an css file into a Django template. 最近几天,我一直在研究如何将css文件包含在Django模板中。 I still did not succeed so am hoping someone can help me out. 我仍然没有成功,所以希望有人能帮助我。 I have the following settings: 我有以下设置:

--settings.py-- --settings.py--

MEDIA_ROOT = '' 
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'

I have not set anything in STATICFILES_DIRS() either. 我也没有在STATICFILES_DIRS()中设置任何内容。

--urls.py-- --urls.py--

urlpatterns = patterns('', (r'^$', 'reviewsite.views.my_homepage_view'),)
urlpatterns += staticfiles_urlpatterns()

--views.py-- --views.py--

def my_homepage_view(request):
return render_to_response('test.html', context_instance=RequestContext(request))

--test.html template-- --test.html模板-

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/style.css"/>

--source code localhost-- -源代码本地主机-

<link rel="stylesheet" type="text/css" href="/static/css/style.css"/>

According to the Django documentation it seems that I have set everything correctly, but the css style is still not applied. 根据Django文档,似乎我已经正确设置了所有内容,但是CSS样式仍然没有应用。 The static folder is in the correct place (C:reviews/reviewsite/static) where the rest of my apps also reside. 静态文件夹位于其他应用程序也正确放置的位置(C:reviews / reviewsite / static)。 Even if I hardcode the style.css location (C:reviews/reviewsite/static/css/style.css) in the test.html template the css style is not applied. 即使我在test.html模板中对style.css位置(C:reviews / reviewsite / static / css / style.css)进行了硬编码,也不会应用CSS样式。 I have checked the style.css and it works without Django. 我已经检查了style.css,它在没有Django的情况下也可以工作。 Any idea of what I am doing wrong? 任何我做错事的想法吗?

This is how you call the files in the static 这就是您在静态文件中调用文件的方式

{% static %}

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

settings.py settings.py

import os
import sys

PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
SITE_ROOT = PROJECT_ROOT

MEDIA_ROOT = os.path.join(SITE_ROOT, 'media') 
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    //create staticfiles folder
    os.path.join(SITE_ROOT, 'staticfiles'),
)

Everyone, thanks for your help and sorry for the late reply. 大家好,谢谢您的帮助,对于您的回复表示抱歉。 I tried your suggestions but it didn't work unfortunately. 我尝试了您的建议,但不幸的是它没有起作用。 However, after some more time trying I got it working now. 但是,经过一段时间的尝试,我现在开始工作了。 This is what worked for me: 这对我有用:

--settings.py-- --settings.py--

MEDIA_ROOT = ''
MEDIA_ROOT = ''
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = 'http://localhost:8000/static/'

--urls.py-- --urls.py--

urlpatterns = patterns('', (r'^$', 'reviewsite.views.my_homepage_view'),)
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT }), )

I now have placed the css file in the static folder in my apps directory. 现在,我将css文件放在我的apps目录中的静态文件夹中。 In the template I am using {{ STATIC_URL }}/style.css. 在模板中,我使用的是{{STATIC_URL}} / style.css。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM