简体   繁体   English

Django模板无法看到CSS文件

[英]Django template can't see CSS files

I'm building a django app and I can't get the templates to see the CSS files... My settings.py file looks like: 我正在构建一个django应用程序,我无法获取模板以查看CSS文件...我的settings.py文件如下所示:

MEDIA_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'media')
MEDIA_URL = '/media/'

I've got the CSS files in /mysite/media/css/ and the template code contains: 我在/ mysite / media / css /中有CSS文件,模板代码包含:

<link rel="stylesheet" type="text/css" href="/media/css/site_base.css" />`

then, in the url.py file I have: 然后,在我的url.py文件中:

# DEVELOPMENT ONLY
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': '/media'}),

but the development server serves the plain html (without styles). 但是开发服务器提供普通的html(没有样式)。 What am I doing wrong? 我究竟做错了什么?

-- -

OK - I got it working based on what you folks have said. 好的 - 我根据你们所说的话让它运转起来。 The answer is: 答案是:

settings.py: settings.py:

MEDIA_ROOT = 'd://web//mysite//media//'  #absolute path to media
MEDIA_URL = '/mymedia/' #because admin already using /media

site_base.html: site_base.html:

<link rel="stylesheet" type="text/css" href="/mymedia/css/site_base.css" />

urls.py urls.py

from mysite import settings
if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^mymedia/(?P<path>.*)$', 'django.views.static.serve',  
         {'document_root':     settings.MEDIA_ROOT}),
    )

And voila! 瞧! It works. 有用。

in the "development only" block in your urls.py you need to change 在urls.py中的“仅限开发”块中,您需要更改

(r'^media/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': '/media'}),

to... 至...

(r'^media/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': settings.MEDIA_ROOT}),

ADMIN_MEDIA_PREFIX is set to \\media\\ by default, and is probably 'stealing' the path. ADMIN_MEDIA_PREFIX默认设置为\\media\\ ,可能是“窃取”路径。 Change that setting, or use a different one for non-admin media - eg site_media or assets . 更改该设置,或对非管理媒体使用不同的设置 - 例如site_mediaassets

On the dev server, I like to cheat and put the following in my urls.py 在开发服务器上,我喜欢欺骗并将以下内容放入我的urls.py中

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^includes/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/static/files'}),
    )

That way anything in the project under the "/includes" folder is server by the dev server. 这样,“/ includes”文件夹下项目中的任何内容都是开发服务器的服务器。 You could just change that to "/media". 您可以将其更改为“/ media”。

It also worked for me, thanks guys !! 这也对我有用,谢谢你们!

settings.py settings.py

MEDIA_ROOT = '/home/pi/ewspaces/ws-classic/xima/media'

MEDIA_URL = '/statics/'

urls.py urls.py

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

inside templates: 内部模板:

<link type="text/css" href="/statics/css/base/jquery.ui.all.css" rel="stylesheet" />

I had a similar problem when I was trying to get jQuery to work. 当我试图让jQuery工作时,我遇到了类似的问题。 My fix was to add an alias to my Apache httpd.conf file that pointed to the folder containing the .js. 我的修复是为我的Apache httpd.conf文件添加一个别名,该文件指向包含.js的文件夹。 You could do the same with your CSS folder. 您可以对CSS文件夹执行相同操作。

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

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