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文件……我的设置。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). What am I doing wrong?
但是开发服务器提供的是纯html(没有样式)。我做错了什么?
--
- - -
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.
瞧!它的工作原理。
5 个解决方案
#1
13
in the "development only" block in your urls.py you need to change
在“只开发”块在您的url。你需要换衣服
(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}),
#2
5
ADMIN_MEDIA_PREFIX
is set to \media\
by default, and is probably 'stealing' the path. Change that setting, or use a different one for non-admin media - eg site_media
or assets
.
ADMIN_MEDIA_PREFIX默认设置为\media\,并可能“盗用”路径。更改该设置,或者为非管理媒体(如site_media或资产)使用不同的设置。
#3
2
On the dev server, I like to cheat and put the following in my urls.py
在dev服务器上,我喜欢作弊,并将以下内容放到我的url .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. You could just change that to "/media".
这样,“/include”文件夹下的项目中的任何内容都是由dev服务器提供的。你可以把它改成"/media"
#4
1
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" />
#5
0
I had a similar problem when I was trying to get jQuery to work. My fix was to add an alias to my Apache httpd.conf file that pointed to the folder containing the .js. You could do the same with your CSS folder.
当我试图让jQuery工作时,我遇到了类似的问题。我的修复是向Apache httpd添加一个别名。指向包含.js的文件夹的conf文件。你也可以对你的CSS文件夹做同样的事情。
#1
13
in the "development only" block in your urls.py you need to change
在“只开发”块在您的url。你需要换衣服
(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}),
#2
5
ADMIN_MEDIA_PREFIX
is set to \media\
by default, and is probably 'stealing' the path. Change that setting, or use a different one for non-admin media - eg site_media
or assets
.
ADMIN_MEDIA_PREFIX默认设置为\media\,并可能“盗用”路径。更改该设置,或者为非管理媒体(如site_media或资产)使用不同的设置。
#3
2
On the dev server, I like to cheat and put the following in my urls.py
在dev服务器上,我喜欢作弊,并将以下内容放到我的url .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. You could just change that to "/media".
这样,“/include”文件夹下的项目中的任何内容都是由dev服务器提供的。你可以把它改成"/media"
#4
1
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" />
#5
0
I had a similar problem when I was trying to get jQuery to work. My fix was to add an alias to my Apache httpd.conf file that pointed to the folder containing the .js. You could do the same with your CSS folder.
当我试图让jQuery工作时,我遇到了类似的问题。我的修复是向Apache httpd添加一个别名。指向包含.js的文件夹的conf文件。你也可以对你的CSS文件夹做同样的事情。