在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[模板标签]django模板变量直接显示html代码的有关问题
- django manage admin css和javascript失效有关问题
- 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 web开发指南》在v1.2.4版本下的出入
- 初记 django 1.4的变化
- django 展示admin登录么有权限
- django template: 输出js数组加逗号的有关问题
- Django(settings.py配备)
- Django + jquery + json 范例实现 在此记录
- django 模板诠释
- Django Admin Site (v1.4) 的Nginx配备
- Django-1.3的helloworld4 写sql话语
- Django,将JavaScript插入模板的语法
- 在 Django 中将 openpyxl 工作簿对象作为 HttpResponse 返回。 是否可以?
- Django Deploy上的内部服务器错误
- django-jenkins-AttributeError:“ Coverage”对象没有属性“ _harvest_data”
- Django:获取数据库错误中最新插入的SPECIFIC字段值:需要多个值才能解压
- Django-根据查询更新或插入
- Django ajax 403因为httponly cookie
- Django:如何将分隔的整数字段(年,月)连接为日期范围以过滤数据库
- django rest 框架中补丁方法的验证
- 安装问题 - Django for python
- django queryset orm-从同一表中获取特定格式的数据
- Django-检查对象是否存在,如果存在则引发错误