姜戈&调试模式打开时的静态文件
当“调试”设置为 True 时,我在模板中的静态文件(图像)方面遇到一些问题:不显示图像。 这是一些设置和模板的代码: http://dpaste.com/594183/
具有这些设置,打印的 html 不包含静态文件的正确路径。无论如何,我记得前段时间,即使路径正确,图像也不会显示,所以问题可能不是路径。
谢谢,卢克
i have some trouble with static files (images) in templates while Debug is set to True: images are not showed.
here is the code of some settings and templates: http://dpaste.com/594183/
with these settings, the printed html doesn't contain the right path to static files. In any case i remember some time ago that even with the right path images are not showed, so maybe the problem is not the path.
thanks, Luke
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你把静态文件放在哪里?
在新的 Django 中,您应该将它们保留在您的应用程序中,例如,如果您有一个名为“blog”的应用程序,请创建一个目录 blog/static 并将文件放在那里。
调试服务器(manage.py runserver)将在 localhost/static/ 上为它们提供服务。
Where do you put your static files?
In a new Django, you should keep them inside your app, e.g. if you have an app named 'blog', create a directory blog/static and put your files there.
Debug server (manage.py runserver) will serve them at localhost/static/.
看到你的粘贴,首先想到的是你的路径中插入了一个空格。在 css 背景图像的 url 中,上下文变量 STATIC_URL 和硬编码路径之间有一个空格。小心 django 中的空格,它的模板引擎可能会对它们变得非常暴躁,并且通常不会为你清理它们。其中一种情况是
{{ value|length }}
,如果您在管道周围插入空格,则该值将无法正确解析为其长度;这是有道理的,因为空格分隔参数。通过删除 CSS 网址中的空格或使用更灵活的 {% static "filename.png" %} 模板标记来解决此问题。查看 https://docs.djangoproject.com/en/dev/howto/ static-files/ 了解更多说明。
还值得注意的是,当您在调试模式之外运行系统时,不会为您管理静态文件。在生产中,您负责自己管理静态文件。
The first thing that comes to mind looking at your paste is that there is a space being inserted into your paths. In the urls for the background images of your css, there is a space between the context variable STATIC_URL and the hardcoded path. Be careful about spaces in django, it's template engine can get very tetchy about them and often won't clean them up for you. One such situation is
{{ value|length }}
, where the value won't be correctly parsed to it's length if you insert spaces around the pipe; This makes sense, since spaces separate arguments. Either remedy this by removing the space in your CSS's Url or use the more flexible {% static "filename.png" %} template tag instead.Check out https://docs.djangoproject.com/en/dev/howto/static-files/ for more explanation.
It's also worth noting that when you run the system outside debug mode that the static files will not be managed for you. In production you are responsible for managing your static files yourself.