2024-04-25 20:00:33 发布
网友
使用finders-module的Django
Django
from django.contrib.staticfiles import finders result = finders.find('css/base.css') searched_locations = finders.searched_locations
字符串result是文件系统路径,如果找不到,请重新检查searched_locations
result
searched_locations
项目目录url添加到文件末尾
if DEBUG: urlpatterns += patterns( '', url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': MEDIA_ROOT}), url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': STATIC_ROOT}), )
项目目录设置
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', ) STATIC_ROOT = os.path.join(BASE_DIR, 'static_debug') STATIC_URL = '/static/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/'
使dirs媒体&;静态调试(将它们添加到.gitignore)
project_dir/ static/ image.jpg settings.py urls.py apps/ __init__.py some_app/ static/ some_app/ css/ style.css media/ static_debug/
现在您可以运行project或直接运行
python manage.py collectstatic
从视图访问
from django.templatetags.static import static static('image.jpg') static('some_app/css/style.css')
从模板访问
{% load staticfiles %} {% static 'image.jpg' %} {% static 'some_app/css/style.css' %}
在犯了很多错误之后,下面的事情对我起了作用。。
假设您有以下目录结构(感谢@madzohan)
project_dir/ static/ image.jpg settings.py urls.py apps/ __init__.py some_app/ static/ some_app/ css/ style.css
现在如果您想获得project dir/static/image.py的路径,那么
在project_dir/settings.py文件中,在文件作用域上定义一个静态的_dir varaible,如。(如果之前没有这样做)
项目目录/settings.py
import os # after your other file variables STATIC_DIR = os.path.join(BASE_DIR, 'static')
现在在您的app_name/views.py文件中,从中访问/static/image.jpg文件
应用程序名/视图.py
import os from project_dir.settings import STATIC_DIR # here I want the /static/image.jpg file then image = os.path.join(STATIC_DIR, 'image.jpg')
然后可以通过图像变量访问image.jpg文件
希望这对你有用
使用finders-module的
Django
字符串
result
是文件系统路径,如果找不到,请重新检查searched_locations
项目目录url添加到文件末尾
项目目录设置
使dirs媒体&;静态调试(将它们添加到.gitignore)
现在您可以运行project或直接运行
从视图访问
从模板访问
在犯了很多错误之后,下面的事情对我起了作用。。
假设您有以下目录结构(感谢@madzohan)
现在如果您想获得project dir/static/image.py的路径,那么
在project_dir/settings.py文件中,在文件作用域上定义一个静态的_dir varaible,如。(如果之前没有这样做)
项目目录/settings.py
现在在您的app_name/views.py文件中,从中访问/static/image.jpg文件
应用程序名/视图.py
然后可以通过图像变量访问image.jpg文件
希望这对你有用
相关问题 更多 >
编程相关推荐