Okey,所以我已经构建了一个有效的角应用程序,我现在正尝试使用django(1.6)进行部署。我有两个问题,首先为什么django服务于静态文件,所以veeery慢? (加载网站需要1分钟,相比之下只使用apache2需要2-5秒)。如何处理我用角度构建的视图? (bunos问题,真的与所有静态文件混淆,有人可以解释我应该在哪里以及如何存储所有静态文件?)
这是文件布局:
var/wwww/
PhotodiceServer (generated from the "startproject" command)/
apache/
django.wsgi
media/
photod (generated from the "startapp" command)/
static/
css/
...
js/
...
scripts/
...
templates/
...
views/
feed.html
...
template/
index.html
admin.py
urls.py
models.py
...
PotodiceServer/
static/
setting.py
urls.py
...
static/
admin/
css
...
这是索引文件:
<!DOCTYPE html>
<html lang="en" ng-app="photodice">
<head>
{% load static from staticfiles %}
<link rel="stylesheet" type="text/css" href={% static "css/bootstrap/bootstrap.min.css" %}>
<link rel="stylesheet" type="text/css" href={% static "css/bootstrapCustomized.css" %}>
<link rel="stylesheet" type="text/css" href={% static "css/PhotodiceMain.css" %}>
<link rel="stylesheet" type="text/css" href={% static "css/newProject.css" %}>
<link rel="stylesheet" type="text/css" href={% static "css/photodiceanimation.css" %}>
<link rel="stylesheet" type="text/css" href={% static "css/myPages.css" %}>
<script type="text/javascript" src={% static "js/fb.js" %}></script>
<script type="text/javascript" src={% static "js/edit.js" %}></script>
<script type="text/javascript" src={% static "js/FileSaver.js" %}></script>
<script type="text/javascript" src={% static "js/jszip.min.js" %}></script>
<script type="text/javascript" src={% static "js/pixastic/pixastic.core.js" %}></script>
<script type="text/javascript" src={% static "js/pixastic/actions/invert.js" %}></script>
<script type="text/javascript" src={% static "js/pixastic/actions/sepia.js" %}></script>
<script type="text/javascript" src={% static "js/pixastic/actions/desaturate.js" %}></script>
<script type="text/javascript" src={% static "js/pixastic/actions/fliph.js" %}></script>
<script type="text/javascript" src={% static "js/pixastic/actions/lighten.js" %}></script>
<script type="text/javascript" src={% static "js/pixastic/actions/blurfast.js" %}></script>
<script type="text/javascript" src={% static "js/pixastic/actions/sharpen.js" %}></script>
<script type="text/javascript" src={% static "js/pixastic/actions/crop.js" %}></script>
<script type="text/javascript" src={% static "js/pixastic/actions/coloradjust.js" %}></script>
<script type="text/javascript" src={% static "js/pixastic/actions/brightness.js" %}></script>
<script type="text/javascript" src={% static "js/pixastic/actions/hsl.js" %}></script>
<script type="text/javascript" src={% static "js/angular/AngularV.1.3.0/angular.min.js" %}></script>
<script type="text/javascript" src={% static "js/angular/angular-ui-router.min.js" %}></script>
<script type="text/javascript" src={% static "js/angular/angular-animate.min.js" %}></script>
<script src={% static "scripts/photodice.js" %}></script>
<script src={% static "scripts/angularLocalStorage.js" %}></script>
<script src={% static "scripts/mainNavCtrl.js" %}></script>
<script src={% static "js/jQuery.js" %}></script>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>PhotoDice</title>
</head>
<body>
<div id="fb-root"></div>
<div id="navbarcontainer">
<div class="viewDiv" ng-include="'templates/nav.html'"></div>
</div>
<div id="contentcontainer">
<div class="viewDiv" ui-view></div>
</div>
<div id="fottercontainer">
</div>
<!--scrips-->
</body>
</html>
这是我添加到/ etcc / apache2中的apache配置文件:
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
WSGIScriptAlias / /var/www/PhotodiceServer/PhotodiceServer/wsgi.py
WSGIPythonPath /var/www/PhotodiceServer
AddHandler wsgi-script .py
<Directory /var/www/PhotodiceServer/PhotodiceServer>
Options +ExecCGI
SetHandler wsgi-script
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
(奖金问题)如果我将上述代码作为一个虚拟服务器放在可用站点文件夹中,结果是否相同?我还要改变什么?
这是urls.py文件(位于photod中):
from django.conf.urls import patterns, url
from django.views.generic import TemplateView
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
url('^$', TemplateView.as_view(template_name='index.html')),
)
urlpatterns += staticfiles_urlpatterns()
来自设置文件:
import os.path
STATIC_ROOT = os.path.normpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'static'))
STATIC_URL = '/static/'
MEDIA_ROOT = '/var/www/PhotodiceServer/media/'
答案 0 :(得分:1)
这完全没有意义。 Django不是你“部署”静态Javascript应用程序的东西。如果你使用Django作为后端并使用Angular作为前端来构建动态数据驱动的应用程序,那将是另一回事,但你似乎并没有这样做。
除了其他任何功能外,Django专门用于提供静态资产(如Javascript文件) 。整个文档都警告不要这样做。即使在普通的Django站点中,您也必须直接通过Web服务器提供JS文件。当然,在您的网站上这样做会让Django完全无关紧要。