django-cms 没有正确路由到静态 url?
我对 Django 还比较陌生,刚刚开始研究 Django-CMS,但是,我无法跳过 官方文档。
我的问题可能与 sekizai 有关,但感觉这是 urls.py 中的基本 url 问题。
基本上,我几乎完全按照教程进行操作。唯一的区别是我的 cms 应用程序位于 blog/ 下,完整路径 ~/workspace/djangocms/blog/。我已在 settings.py 下正确设置了 STATIC_URL
和 STATIC_ROOT
,我的 MEDIA_URL
和 MEDIA_ROOT
也是如此。
我调出了媒体路径和目录,因为如果我将基本模板设置为链接到 {{ MEDIA_URL }}css/somecss.min.css
处的 css,它就可以正常工作。但是,对 STATIC_URL 执行相同的操作: {{ STATIC_URL }}css/somecss.min.css
不起作用并会产生 404。
另外,据我所知,默认的 /static/ 路由似乎适用于其他目录。 {% cms_toolbar %}
生成的代码生成良好,并且来自 /static/cms/css/plugins/cms.toolbar.css 等位置的 css 已正确提供。
urls.py 的内容
from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'blog.views.home', name='home'),
# url(r'^blog/', include('blog.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT,
'show_indexes': True}),
url(r'',
include('django.contrib.staticfiles.urls')),
) + urlpatterns
I'm still relatively new to Django and have just started poking around at Django-CMS, however, I can't get past the basic introduction from the official docs.
My problem may be related to sekizai, but it feels like it's a basic url issue in urls.py.
Basically, I have followed the tutorial almost exactly. The only difference is that I have my cms app under blog/, full path ~/workspace/djangocms/blog/. I have set the STATIC_URL
and STATIC_ROOT
properly under settings.py and the same goes with my MEDIA_URL
and MEDIA_ROOT
.
I bring up the media path and directory because if I set my base template to link to css at {{ MEDIA_URL }}css/somecss.min.css
it works fine. However, doing the same with STATIC_URL: {{ STATIC_URL }}css/somecss.min.css
doesn't work and produces 404s.
Also, from what I can tell, the default /static/ routes seem to work fine for other directories. The code produced by {% cms_toolbar %}
generates fine and css from places like /static/cms/css/plugins/cms.toolbar.css are being served properly.
Contents of urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^
, 'blog.views.home', name='home'),
# url(r'^blog/', include('blog.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)
, 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT,
'show_indexes': True}),
url(r'',
include('django.contrib.staticfiles.urls')),
) + urlpatterns
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在遵循相同的教程后,我反复解决这个问题。
我的网站将加载标准欢迎页面,并且我可以创建 CMS 页面。但是,当我尝试在模板中加载静态文件时,我不断收到 404 错误。
OP给出的答案对我帮助很大。
需要明确的是,这对我有用:
将项目中的静态目录重命名为 local_static 之类的内容
将以下内容添加到 settings.py< /p>
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, "local_static/"),
)
这对我有用。我现在可以停止拔头发了
I went round and round with this problem after following the same tutorial.
My site would load the standard welcome page and I could create CMS pages. However, when I tried to load static files in my templates I kept getting 404 errors.
The answer given by the OP helped me a lot.
Just to be clear, this worked for me:
Rename the static directory in the project to something like local_static
Add the following to settings.py
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, "local_static/"),
)
This worked for me. I can now stop pulling my hair out
从 freenode 上的 #django 得到了我的答案。在这里我自己回答,以防有人需要它:
主要用于 django-cms,STATIC_ROOT 主要用于生产过程中来自 django-cms 本身的资产。如果 django 使用 python manage.py runserver 方法运行,则 STATIC_ROOT 无关紧要。
来自 Django 文档:使用
collectstatic
收集所有使用的静态资源(对于 django-cms 来说,这些资源可能位于 /usr/local/lib/python##/dist-packages 等目录下)。这显示了 STATIC_ROOT 将在生产过程中使用哪些文件。如果您想使用 STATIC_URL 为其他目录提供服务,则必须将它们添加到
settings.py
的STATICFILES_DIRS
部分。 (请务必阅读注释——使用绝对路径而不是相对路径)。在制作纯 Django 应用程序时,这实际上相对明显,但是,因为我想严格遵循 Django-cms 教程,所以这并不像应有的那么明显。Got my answer from #django on freenode. Answering it myself here in case anyone needs it:
Mainly for django-cms, STATIC_ROOT is mainly used for assets from django-cms itself during production. If django is being run using the
python manage.py runserver
method, STATIC_ROOT is irrelevant.From the Django docs: Use
collectstatic
to collect all the static assets used (which for django-cms will probably be under a directory like /usr/local/lib/python##/dist-packages). This shows which files STATIC_ROOT will use during production.If you want to serve other directories with STATIC_URL, you will have to add them to the
STATICFILES_DIRS
section ofsettings.py
. (Make sure to read the comments -- use the absolute paths and not relative paths). This is actually relatively obvious when making a pure Django app, however, because I wanted to strictly follow the Django-cms tutorial this was not as obvious as it should've been.解决方案是如此简单,我也错过了。
来自设置.py;
如果您查看“mysitename”内部,会发现另一个名为“static”的文件夹。如果您将文件放在那里,一切都会按照您的预期进行。与manage.py同一级别的“static”文件夹是一个转移注意力的文件夹。
The solution is so simple I missed it too.
from settings.py;
If you look inside 'mysitename' there is another folder named 'static'. If you place your files there, everything will work as you may expect it to. The 'static' folder in the same level as manage.py is a red herring.
我在此处发布了相同的答案,但我遇到了同样的问题。虽然我能够为其他所有内容提供静态文件,但由于某种原因,我的 django-cms 文件不断返回 404。我检查了 settings.py 和权限,但运气不佳。
在运行 Nginx 和 Gunicion 的 Ubuntu 14.04 上,我能够通过编辑解决问题:
我添加了以下代码块
然后我重新启动了 Nginx 和 Gunicorn
,我能够看到所有丢失的静态文件
I posted the same answer here, but I was encountering the same issues. While I was able to serve static files for everything else, for some reason my django-cms files kept returning a 404. I checked settings.py and permissions but I did not have much luck.
On Ubuntu 14.04 running Nginx and Gunicion I as able to solve the problem by editing:
And I added the following code block
Then I restarted Nginx and Gunicorn
and I was able to see all the missing static files