1

我在 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)

它在我的本地运行良好。我应该知道什么?

4

2 回答 2

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 项目工作正常。

于 2013-05-28T13:16:21.590 回答
2

urls.py中,当您使用:

static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

您需要MEDIA_URLsettings.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),
    ]
于 2015-05-14T19:28:04.850 回答