Django & static files while Debug mode is ON

六月ゝ 毕业季﹏ 提交于 2019-12-11 15:13:21

问题


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


回答1:


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/.




回答2:


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.



来源:https://stackoverflow.com/questions/7057982/django-static-files-while-debug-mode-is-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!