django 中显示图片,使用静态文件的问题(django static files)
在django1.3中,提供了django.contrib.staticfiles这个模块,方便使用静态文件,显示图片,使用css等。
在设置时需要注意的是这几个常量定义:
--------------------------------------------------------------------------------------------
1.MEDIA_ROOT MEDIA_URL
MEDIA_ROOT 保存用户上传的文件的路径
MEDIA_URL 表示从web根目录开始的URL映射
2.STATIC_ROOT STATIC_URL
含义同上。
3.ADMIN_MEDIA_ROOT
4.STATICFILES_DIRS
项目相关的静态文件夹列表。注意不同的目录下不能有重名的文件,否则只会找到先搜到的那个返回。
在settings.py中:
--------------------------------------------------------------------------------------------
HERE=os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT=os.path.join( HERE , 'media').replace('\\','/')
MEDIA_URL = '/media/'
STATIC_ROOT =os.path.join( HERE , 'static').replace('\\','/')
STATIC_URL= '/static/'
ADMIN_MEDIA_ROOT = '/static/admin/'
STATICFILES_DIRS = (
os.path.join(HERE,'app1/static/').replace('\\','/'),
os.path.join(HERE,'app2/static/').replace('\\','/')
)
在 urls.py中添加:
--------------------------------------------------------------------------------------------
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL , document_root = settings.MEDIA_ROOT )
urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT )
in templates: 【注意,1.5已经变了,见末尾补充部分】
--------------------------------------------------------------------------------------------
{% load static %}
<img src="{{ get_static_prefix }}images/1.jpg" />
<link href="{{ get_static_prefix }}css/truple.css />
很重要的几段话:
--------------------------------------------------------------------------------------------
1.# Don't put anything in this directory(STATIC_ROOT) yourself; store your static files in apps' "static/" subdirectories and in STATICFILES_DIRS.
2.The most likely example is user-uploaded content in MEDIA_ROOT. staticfiles is intended for static assets and has no built-in handling for user-uploaded files, but you can have Django serve your MEDIA_ROOT by appending something like this to your URLconf:
urlpatterns += static(settings.MEDIA_URL , document_root = settings.MEDIA_ROOT )
在django1.5中,上传图片的设置是这样的:
--------------------------------------------------------------------------------------------
在models.py中,
photo=models.ImageField(upload_to=settings.MEDIA_ROOT)
在模板中
首先{% load staticfiles %} !注意,不用于1.3的static
显示图片: <img src="{{ obj.photo.url }}"/>
参考资料:
--------------------------------------------------------------------------------------------
http://docs.djangoproject.com/en/dev/howto/static-files/
http://stackoverflow.com/questions/10474639/failing-to-show-images-in-templates-django
在设置时需要注意的是这几个常量定义:
--------------------------------------------------------------------------------------------
1.MEDIA_ROOT MEDIA_URL
MEDIA_ROOT 保存用户上传的文件的路径
MEDIA_URL 表示从web根目录开始的URL映射
2.STATIC_ROOT STATIC_URL
含义同上。
3.ADMIN_MEDIA_ROOT
4.STATICFILES_DIRS
项目相关的静态文件夹列表。注意不同的目录下不能有重名的文件,否则只会找到先搜到的那个返回。
在settings.py中:
--------------------------------------------------------------------------------------------
HERE=os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT=os.path.join( HERE , 'media').replace('\\','/')
MEDIA_URL = '/media/'
STATIC_ROOT =os.path.join( HERE , 'static').replace('\\','/')
STATIC_URL= '/static/'
ADMIN_MEDIA_ROOT = '/static/admin/'
STATICFILES_DIRS = (
os.path.join(HERE,'app1/static/').replace('\\','/'),
os.path.join(HERE,'app2/static/').replace('\\','/')
)
在 urls.py中添加:
--------------------------------------------------------------------------------------------
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL , document_root = settings.MEDIA_ROOT )
urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT )
in templates: 【注意,1.5已经变了,见末尾补充部分】
--------------------------------------------------------------------------------------------
{% load static %}
<img src="{{ get_static_prefix }}images/1.jpg" />
<link href="{{ get_static_prefix }}css/truple.css />
很重要的几段话:
--------------------------------------------------------------------------------------------
1.# Don't put anything in this directory(STATIC_ROOT) yourself; store your static files in apps' "static/" subdirectories and in STATICFILES_DIRS.
2.The most likely example is user-uploaded content in MEDIA_ROOT. staticfiles is intended for static assets and has no built-in handling for user-uploaded files, but you can have Django serve your MEDIA_ROOT by appending something like this to your URLconf:
urlpatterns += static(settings.MEDIA_URL , document_root = settings.MEDIA_ROOT )
在django1.5中,上传图片的设置是这样的:
--------------------------------------------------------------------------------------------
在models.py中,
photo=models.ImageField(upload_to=settings.MEDIA_ROOT)
在模板中
首先{% load staticfiles %} !注意,不用于1.3的static
显示图片: <img src="{{ obj.photo.url }}"/>
参考资料:
--------------------------------------------------------------------------------------------
http://docs.djangoproject.com/en/dev/howto/static-files/
http://stackoverflow.com/questions/10474639/failing-to-show-images-in-templates-django
热门话题 · · · · · · ( 去话题广场 )
- 解锁我的夏日旅行足迹地图 活动 19.6万次浏览
- 我给自己写了一个使用说明书 新话题
- 你最爱的粽子口味 2.4万次浏览
- 夏季赏花指南 新话题
- 第26届上海国际电影节 8.3万次浏览
- 用对话体写日记 1547次浏览