django发布在apache2 django上,请问如何用apache2 django解析静态文件

在django1.7.5中访问静态文件(js css)
刚开始参考的是别的文章,后来参考文章《各种 django 静态文件的配置总结》才看到原来没有但是没有注意到版本,折腾了一晚上,浪费了很多很多时间.后来终于知道搜索django1.7访问静态文件.真是傻×.
python 2.7.3
django 1.7.5
django是不善于处理静态文件这种事情的.这样的工作要交给nginx或者apache这样的服务器.但是在调试时还是要配置一下的
django 1.7.5配置访问静态文件貌似比其他的版本都要简单一些.只需要如下步骤:
收集静态文件,然后放在app下的static中,目录如下:
├── admin.py
├── admin.pyc
├── forms.py
├── forms.pyc
├── __init__.py
├── __init__.pyc
├── migrations
├── 0001_initial.py
├── 0001_initial.pyc
├── 0002_auto_3.py
├── 0002_auto_3.pyc
├── __init__.py
└── __init__.pyc
├── models.py
├── models.pyc
├── static
├── css
├── bootstrap.css
├── bootstrap.css.map
├── bootstrap.min.css
├── bootstrap-theme.css
├── bootstrap-theme.css.map
└── bootstrap-theme.min.css
├── fonts
├── glyphicons-halflings-regular.eot
├── glyphicons-halflings-regular.svg
├── glyphicons-halflings-regular.ttf
├── glyphicons-halflings-regular.woff
└── glyphicons-halflings-regular.woff2
├── images
├── chrome.png
├── firefox.png
├── ie.png
├── opera.png
└── safari.png
├── bootstrap.js
├── bootstrap.min.js
├── html5shiv.js
├── html5shiv.min.js
├── jquery-1.11.1.js
├── jquery-1.11.1.min.js
├── jquery-1.11.1.min.map
├── jquery-1.11.2.min.js
├── jquery-1.11.2.min.map
├── npm.js
└── respond.min.js
├── templates
├── base.html
├── buttons.html
├── contact.html
├── form.html
├── form_inline.html
├── formset.html
├── form_using_template.html
├── index.html
├── login.html
├── pagination.html
└── tabs.html
├── tests.py
├── views.py
└── views.pyc
设置setting.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/'),
'/var/www/static',
在模版中使用
{% load staticfiles %} & img src = "{% static "img/test.jpg" %}" alt = "test"/&
{% load staticfiles %} & link rel="stylesheet" href="{% static 'css/header.css' %}" &
确保setting.py中的INSTALL_APPS中包含django.contrib.staticfiles
确保setting.py中的DEBUG选项为True,否则无法映射到静态文件目录
在生产模式中不可设置为True哦,所以生产模式下的配置是不一样的,下面使用nginx来配置静态资源.
说明一下,有关几个settings中设置的问题 static_root static_url staticfiles_dirs:
settings.py中
As for the static folder under myproject root, actually it doesn’t necessarily have to be there. As long as STATIC_ROOT (in settings.py) points to the same location. So what’s going on here is when you do:
python manage.py collectstatic
It will copy all the individual static folder to the STATIC_ROOT folder, that’s why in each of your app, you need to follow the naming convention like this:
app_one/static/app_one/css
You are not supposed to put stuff directly under the app’s static folder, which might cause name collisions when you collectstatic.
也就是说.在执行collectstatic时,会自动的把每个app下的static文件拷贝到static_root指定的路径下.另外采用这样的目录格式app_one/static/app_one,这样在有多个app的情况下.可以就不会因为有文件名相同的文件被覆盖掉
the STATIC_URL in the settings.py
This url will appear in your browser:
(BTW, make sure the URL ends with a slash/, without a slash it might work but very unstable, django will error out this in the log)
# STATIC_URL = '/static/' # it doesn't have to this
STATIC_URL = '/static/monkeyking/' # it can be anything
You shouldn’t hardcode the img or css in your template something like:
... url = "/static/app_one/css/mycss.css" ...
... url = "../../static/app_one/img/logo.png" ...
In stead, you should do this in your template:
{% load staticfiles %}
& link rel="stylesheet" href="{% static "gl_pulltabs/css/foundation.css" %}" / &
这个STATIC_URL的作用其实是:在settings.py中DEBUG和TEMPLATE_DEBUG都为开的情况下.只要在模版中像上面这样使用,就会自动的在前面加上/static/.例如你想要请求的是/static/app_one/css/mycss.css,那么如果你讲STATIC_URL设置为/static/appone/,那么你只要使用{% static “css/mycss.css” %}就可以了.
在开发阶段,Django把/static映射到django.contrib.staticfiles这个App。staticfiles自动地从STATICFILES_DIRS、STATIC_ROOT以及各个App的static子目录里面搜索静态文件。一旦布署到开发环境上,settings.py不需要重新编写,只要在Apache的配置文件里面写好映射,/static将会被Apache处理。django.contrib.staticfiles虽然仍然存在,但因为不会接收到以/static/开始的路径,所以将不会产生作用。不必担心Django会使用处理速度变慢。另外,当settings.DEBUG is False的时候,staticfiles将自动关闭。
STATICFILES_DIRS
Here, in settings.py again, you need to explicitly tell django where else to look for static files:
STATICFILES_DIRS = (
"/path/to/your/project/yourapp/static/",
Can you put your deploy static folder(STATIC_ROOT) path to here, so you can save some disk space? No, you cannot! django won’t allow it.
此条目发表在分类目录。将加入收藏夹。
2016年二月 &(8)
2016年一月 &(11)
2015年十二月 &(19)
2015年十一月 &(20)
2015年十月 &(10)
2015年九月 &(9)
2015年八月 &(24)
2015年七月 &(15)
2015年六月 &(32)
2015年五月 &(20)
2015年四月 &(33)
2015年三月 &(44)
2015年二月 &(23)
2015年一月 &(74)
2014年十二月 &(88)
2014年十一月 &(21)
2014年九月 &(6)
2016年三月
78910111213
14151617181920
21222324252627
分类目录分类目录
选择分类目录
一些问题&&解决方法&&(24)
人工智能&&(15)
&&&专家系统&&(14)
嵌入式&&(1)
bootstrap&&(5)
c & c++&&(9)
command&&(39)
Contiki&&(9)
算法相关&&(4)
网络相关&&(20)
Django&&(25)
driver&&(2)
emotion&&(11)
读书笔记&&(26)
freeshell&&(11)
Html&&(14)
makefile&&(1)
nginx&&(4)
paper notes&&(4)
python&&(61)
shell脚本&&(15)
software configure&&(51)
the knowledge u need know&&(104)
ubuntu&&(83)
windows&&(3)
未分类&&(12)
收集的信息&&(10)[D]如何将Django应用从自带的服务器转移至Apache上?
[问题点数:80分,结帖人kelamayi]
[D]如何将Django应用从自带的服务器转移至Apache上?
[问题点数:80分,结帖人kelamayi]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
本帖子已过去太久远了,不再提供回复功能。Django处理静态文件方法
开发网站的过程中,我们需要使用到大量的静态文件,例如js,CSS,图片等等,一般我们会在网页中以URL的形式引用它们。Django提供了一些特殊的机制来处理静态文件。默认情况下(如果没有修改STATICFILES_FINDERS的话),Django首先会在STATICFILES_DIRS配置的文件夹中寻找静态文件,然后再从每个app的static子目录下查找,并且返回找到的第一个文件。所以我们可以将全局的静态文件放在STATICFILES_DIRS配置的目录中,将app独有的静态文件放在app的static子目录中。存放的时候按类别存放在static目录的子目录下,如图片都放在images文件夹中,所有的CSS都放在css文件夹中,所有的js文件都放在js文件夹中。
配置全局的静态文件目录
默认情况下,STATICFILES_DIRS为空,我们可以这样配置:
import&os.path
STATICFILES_DIRS&=&(
&&&&#&Put&strings&here,&like&"/home/html/static"&or&"C:/www/django/static".
&&&&#&Always&use&forward&slashes,&even&on&Windows.
&&&&#&Don't&forget&to&use&absolute&paths,&not&relative&paths.
&&&&os.path.join(os.path.dirname(__file__),&'static').replace('\\','/'),
然后手动创建static文件夹,效果如图:
配置app的静态文件目录
直接在每个app目录下创建static目录,将文件直接放进去就可以了。如果Django在STATICFILES_DIRS当中没有找到的话,会到app的目录下的static目录去查找的。
假设我们在STATICFILES_DIRS配置的目录中,放了一个Chrome.jpg图片文件,在网页中可以通过/static/Chrome.jpg来引用。当然这是默认情况,/static/这个前缀可以通过配置文件中的STATIC_URL来修改。
另外,Django提供了一个findstatic命令来查找指定的静态文件所在的目录,例如:D:\TestDjango&python&manage.py&findstatic&Chrome.jpg
('D:/TestDjango/TestDjango/templates',)
Found&'Chrome.jpg'&here:
&&D:\TestDjango\TestDjango\static\Chrome.jpg
以上是开发过程中关于静态文件的配置,注意是开发阶段而不是部署阶段。注意到关于静态文件还有一个配置变量STATIC_ROOT,这是干嘛的呢?当工程部署之后,处理静态文件较好的方法是交给Web容器来做,比如Apache,所以我们要将所有的静态文件放在一起,然后交给Apache托管,Django提供了一个命令collectstatic用来把所有的静态文件收集到STATIC_ROOT当中。所以有一个问题就是,所有的静态文件,最好不要有重名,笔者尚不了解重名会有什么结果。
参考资料:
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。django发布在apache上,请问如何用apache解析静态文件_百度知道
django发布在apache上,请问如何用apache解析静态文件
提问者采纳
我用django+mod_python的方法把程序发布到apache上了。我从网上看到说可以使用apache服务器代替,不过对应静态文件(如图片,js,css等文件)的解析还是使用的django如题
其他类似问题
为您推荐:
django的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 centos django apache 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信