我在ubuntu上安装了MySQL和mod_wsgi的Django 1.5.1。将项目从本地计算机导入服务器后,我通过访问所有URL得到此错误:
**ImproperlyConfigured at /**
Empty static prefix not permitted
Request Method: GET
Request URL: http://50.7.177.122/
Django Version: 1.5.1
Exception Type: ImproperlyConfigured
Exception Value:
Empty static prefix not permitted
Exception Location: /srv/projects/venv/lib/python2.7/site-packages/django/conf/urls/static.py in static, line 22
Python Executable: /usr/bin/python
Python Version: 2.7.3
Python Path:
['/srv/projects/app/danial',
'/srv/projects/app',
'/srv/projects/venv/lib/python2.7/site-packages',
'/srv/projects/venv/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
'/srv/projects/venv/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg',
'/srv/projects/venv/lib/python2.7/site-packages',
'/srv/projects/venv/lib/python2.7/site-packages/PIL',
'/usr/local/lib/python2.7/dist-packages/virtualenv-1.9.1-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/virtualenvwrapper-4.0-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/stevedore-0.8-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/virtualenv_clone-0.2.4-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/pip-1.3.1-py2.7.egg',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages']
我的项目名称是danial,它包含一个应用程序,图库。我的静态文件夹位于/ danial / gallery / static我很困惑。这是我项目中所有静态相关的行:
###settings.py
STATIC_ROOT = '/srv/projects/app/danial/gallery/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = ('/srv/projects/app/danial/gallery/static/',)
###apache setting
Alias /static/ /srv/projects/venv/lib/python2.7/site-packages/django/contrib/admin/static/
###urls.py
from danial import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
它适用于我的本地。我知道什么?
答案 0 :(得分:2)
我已经更改了静态别名
/srv/projects/venv/lib/python2.7/site-packages/django/contrib/admin/static/
到/srv/projects/app/danial/gallery/static/
并从urls.py
删除了与静态相关的行。
我只需要将../django/../static/admin
复制到../projects/../static/admin
。现在django项目工作正常。
答案 1 :(得分:2)
在urls.py
中,当您使用时:
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
您需要在MEDIA_URL
中定义settings.py
没有空值。
如果您使用的话,请在STATIC_URL
中定义settings.py
没有空值:
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
当DEBUG
设置为True
时(开发期间),它也可以正常工作,
以下是sattic
的源代码:
def static(prefix, view=serve, **kwargs):
if not settings.DEBUG or (prefix and '://' in prefix):
return []
elif not prefix:
raise ImproperlyConfigured("Empty static prefix not permitted")
return [
url(r'^%s(?P<path>.*)$' % re.escape(prefix.lstrip('/')), view, kwargs=kwargs),
]