【发布时间】:2011-07-31 09:30:14
【问题描述】:
这不像其他人那么简单的问题.. 至少。设置简单的基于单目录的静态文件位置不是问题..
https://bitbucket.org/sirex/django-starter/src
这里有一个非常有趣的项目.. 这个项目使用分发和构建来制作整个项目和 django 与一个目录中的模块。您可以轻松地从开发模式迁移到生产模式等等。您所需要的只是重命名 dir,然后在其中输入“make”,就是这样 =) 那里有手册...
适用于 python 服务器的情况,不适用于 apache mod_wsgi: 默认静态文件位置是:“var/htdocs/static”。这可以被一个静态目录位置覆盖,例如apps/myapp/myapp/static/。这适用于 python webserver,但不适用于 wsgi/apache。 wsgi 除了默认目录之外什么都看不到.. 示例:http://localhost:8000/static/css/main.css 有效,但与 apache 相同的 url 无效。这个文件位于 myproject/apps/myapp/myapp/static/css/main.css 虽然默认的静态目录是 var/htdocs/static =)
据我了解,在 settings.py 中使用 StaticFiles 应用程序进行的覆盖
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BUILDOUT_DIR, 'var', 'htdocs', 'static')
STATICFILES_DIRS = (
os.path.join(BUILDOUT_DIR, 'project', 'static'), # <-- why "project" and not "apps" I don't know X_X
)
也许这个不正确,我不知道,但使用 py-server 可以。 apache vhost 使用默认位置.. 并设置为“var/htdocs/static”。
也许问题出在 wsgi 脚本中?
#!/usr/local/bin/python2.6
import os,sys
sys.path[0:0] = [
'/usr/local/lib/python2.6/site-packages/',
'/www/webapp/visimes/eggs/PIL-1.1.7-py2.6-freebsd-8.1-RELEASE-amd64.egg',
'/www/webapp/visimes/eggs/South-0.7.3-py2.6.egg',
'/www/webapp/visimes/eggs/django_annoying-0.7.6-py2.6.egg',
'/www/webapp/visimes/eggs/coverage-3.4-py2.6-freebsd-8.1-RELEASE-amd64.egg',
'/www/webapp/visimes/eggs/django_debug_toolbar-0.8.4-py2.6.egg',
'/www/webapp/visimes/eggs/django_extensions-0.6-py2.6.egg',
'/www/webapp/visimes/eggs/django_test_utils-0.3-py2.6.egg',
'/www/webapp/visimes/eggs/ipdb-0.3-py2.6.egg',
'/www/webapp/visimes/eggs/ipython-0.10.1-py2.6.egg',
'/www/webapp/visimes/eggs/djangorecipe-0.21-py2.6.egg',
'/www/webapp/visimes/eggs/zc.recipe.egg-1.3.2-py2.6.egg',
'/www/webapp/visimes/eggs/zc.buildout-1.5.2-py2.6.egg',
'/www/webapp/visimes/eggs/BeautifulSoup-3.2.0-py2.6.egg',
'/www/webapp/visimes/eggs/setuptools-0.6c12dev_r88795-py2.6.egg',
'/www/webapp/visimes/parts/django',
'/www/webapp/visimes',
'/www/webapp/visimes/project', # <-- this one need for monitor.py which i put in there
'/www/webapp/visimes/apps/portal', # <-- startapp.sh script some how forgot to add this dir, it's my default app dir, which must be generated with startapp.sh and added in here..
]
import djangorecipe.wsgi
if __name__ == '__main__':
djangorecipe.manage.main('project.development')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.development'
import monitor
monitor.start(interval=1.0)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
我自己添加了最后 4 行。因为我无法启动 apache .. 我猜 djangorecipe.wsgi 应该使用 staticFile 覆盖来处理其他所有事情.. 无论如何,如果你在 linux 或 mac 上,请检查那个包,然后自己尝试。它必须工作
ps。 (顺便说一下 bin/django 需要复制为 bin/django.wsgi 和 etc/apache.conf 是为 apache 生成的 vhost)
如果有人尝试使用 wsgi 手动启动这个“Starter”,我真的很高兴......那么你就会明白一切。=)
编辑:任何关于 WSGI 如何理解除了 django 设置的默认位置之外他需要在哪里搜索静态文件的信息,非常感谢 =)
【问题讨论】:
-
您将 apache 配置中的别名指向 /your/path/to/static/? 中的服务器静态文件?
-
这个呢? docs.djangoproject.com/en/dev/ref/contrib/staticfiles/… 这东西是怎么工作的?它可以从多个位置收集静态文件?
-
再一次,如果您将别名添加到指向托管静态文件的位置的 Apache 配置,则 Apache(或任何其他网络服务器)只能提供静态文件。是的,您应该使用 collectstatic 命令将所有静态文件收集到一个位置(为了方便或 bcs。您不想将许多别名设置到不同的位置)。 staticfiles 应用程序主要用于帮助您在本地开发服务器上进行开发,而无需执行诸如 collectstatic 之类的操作或在服务器配置中添加别名。在生产环境中,唯一的用法是在模板中包含 STATIC_URL。
-
请参阅docs.djangoproject.com/en/dev/howto/deployment/modwsgi/…,了解有关如何在生产中处理媒体/静态文件的更多信息。