在django1.3中,提供了django.contrib.staticfiles这个模块,方便使用静态文件,显示图片,使用css,js等。
在设置时需要注意的是:
1.MEDIA_ROOT??MEDIA_URL
2.STATIC_ROOT STATIC_URL
3.ADMIN_MEDIA_ROOT
4.STATICFILES_DIRS
In 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('\\','/')
)
In urls.py add these codes:
from django.conf import settings
from djagno.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:
{% 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 )
详细解决方案
Django运用静态文件
热度:168 发布时间:2013-01-28 11:49:56.0
相关解决方案
- django 小学识
- django rest framework 入门二——Request and Response
- django[模板标签]django模板变量直接显示html代码的有关问题
- django manage admin css和javascript失效有关问题
- [django]依据model生成html table
- Django 的css跟js压缩插件:django_compressor
- 3 Django 1.5.4 TinyMCE
- 5 Django 1.5.4 User Authentication 用户认证
- 1 Django 1.5.4 入门
- Django 1.5.4 课题25 Notifications
- Django 1.5.4 课题23 Celery RabbitMQ
- Django 1.5.4 课题六 login 和 logout
- Django 1.5.4 课题八 Model
- Django 1.5.4 课题二 urls 和 view 提高
- django restful webservice回到json数据
- 《django web开发指南》在v1.2.4版本下的出入
- 初记 django 1.4的变化
- 利用 Django 与 jQuery 回创建电子表格应用程序
- django 展示admin登录么有权限
- django template: 输出js数组加逗号的有关问题
- Django-1.3的helloworld7 高速复习
- Django(settings.py配备)
- Django + jquery + json 范例实现 在此记录
- Django 分页插件:django-pagination
- django 模板诠释
- django book-学习札记(第三章)
- Django Admin Site (v1.4) 的Nginx配备
- Django Signal施用
- Django 学习1 eclipse+PyDev运行Django
- django RestFrameWork的容易测试