我正在使用 django 1.5
我能够在生产中提供这些文件,因为它是在 apache 级别处理的。这是我的 httpd.conf 文件:
<VirtualHost *:80>
WSGIScriptAlias / /home/membership/membership/wsgi.py
Alias /static/ "/home/membership/static/"
<Directory /home/membership/static>
Order deny,allow
Allow from all
</Directory>
<Directory "/usr/lib/python2.6/site-packages/django/contrib/admin/static/admin">
Order deny,allow
Allow from all
</Directory>
<Directory /home/membership/membership>
<Files wsgi.py>
Order deny,allow
Satisfy Any
Allow from all
</Files>
</Directory>
</VirtualHost>
这在生产中效果很好,因为Alias /static/ "/home/membership/static/"
当我尝试在本地开发环境中运行该应用程序时,我无法让它提供静态文件,我只是收到页面未找到 404 错误。我猜这是因为当我在本地开发时,请求会直接发送到开发服务器,因为没有使用 apache。
我确实有一个文件 /static/me.png 。
我应该在开发中指定某个地方来提供静态文件吗?
跑步时python manage.py collectstatic
它似乎只收集管理应用程序的静态文件。我直接在 /app/static 目录中有一个文件,我正在尝试提供服务。
如果您在开发期间提供静态文件
On your 设置.py file:
# Add it on your settings.py file
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"), # your static/ files folder
]
Example:
在你的根上urls.py file:
# add these lines
from django.conf import settings
from django.conf.urls.static import static
# Add +static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Example:
这不适合生产使用!查看更多:https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-during-development https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-during-development
对于媒体文件media/目录:https://docs.djangoproject.com/en/dev/howto/static-files/#serving-files-uploaded-by-a-user-during-development https://docs.djangoproject.com/en/dev/howto/static-files/#serving-files-uploaded-by-a-user-during-development
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)