不正确配置/空静态前缀不允许 - Django [英] ImproperlyConfigured at / Empty static prefix not permitted - Django
问题描述
我正在使用Django上传/显示图像。
I'm working on uploading/displaying images with Django.
该网站部署在Heroku上。
The website is deployed on Heroku.
按照此教程,我能够成功上传图片。
Following this tutorial I was able to successfully upload images.
但是,图片没有显示在模板中。
However, the images weren't being displayed in the template.
然后我了解到我的urls.py应该在末尾有这一行:
I then learned that my urls.py should have this line at the end:
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我把这个添加到我的urls.py的末尾,但是现在我得到这个错误: p>
I added this to the end of my urls.py but now I'm getting this error:
ImproperlyConfigured at / Empty static prefix not permitted
我的settings.py中有MEDIA_URL和MEDIA_ROOT,并且都不为空。
I have MEDIA_URL and MEDIA_ROOT in my settings.py and neither are empty.
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'
为什么会出现此错误,我该如何解决?
Why is this error happening and how would I fix it?
我认为是我的urls.py的相关部分:
Here's what I think is the relevant part of my urls.py:
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
import notifications
admin.autodiscover()
urlpatterns = patterns('',
....urls......
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
推荐答案
我在我的urls.py中添加了相同的行,并得到与您相同的错误。
I added the same line in my urls.py and got the same error as you.
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
文档 here 表示使用 settings.STATIC_URL
和 settings.STATIC_ROOT
我将其更改为文档版本
urlpatterns = patterns('',
....urls......
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
,错误消失了!
我检查了我的设置文件,确认 settings.MEDIA_URL
和 settings.MEDIA_ROOT
都被正确定义。之后我调整了urls.py回到使用 settings.MEDIA_URL
和 settings.MEDIA_ROOT
。一切都按预期工作。
I checked my settings file and made sure settings.MEDIA_URL
and settings.MEDIA_ROOT
were both defined correctly. Later I adjusted urls.py back to using settings.MEDIA_URL
and settings.MEDIA_ROOT
. Everything worked as expected.
这些是我的settings.py文件的相关部分:
These are the relevant parts of my settings.py file:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
REPOSITORY_ROOT = os.path.dirname(BASE_DIR)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(REPOSITORY_ROOT, 'static/')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(REPOSITORY_ROOT, 'media/')
我认为 MEDIA_URL
设置不正确
这篇关于不正确配置/空静态前缀不允许 - Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!