Python ImproperlyConfigured at / Empty static prefix not allowed - Django
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23874122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImproperlyConfigured at / Empty static prefix not permitted - Django
提问by Daniel Scott
I'm working on uploading/displaying images with Django.
我正在使用 Django 上传/显示图像。
The website is deployed on Heroku.
该网站部署在 Heroku 上。
Following thistutorial I was able to successfully upload images.
按照本教程,我能够成功上传图像。
However, the images weren't being displayed in the template.
但是,图像没有显示在模板中。
I then learned that my urls.py should have this line at the end:
然后我了解到我的 urls.py 最后应该有这一行:
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I added this to the end of my urls.py but now I'm getting this error:
我将此添加到 urls.py 的末尾,但现在出现此错误:
ImproperlyConfigured at / Empty static prefix not permitted
I have MEDIA_URL and MEDIA_ROOT in my settings.py and neither are empty.
我的 settings.py 中有 MEDIA_URL 和 MEDIA_ROOT 并且都不是空的。
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?
为什么会发生此错误,我该如何解决?
Here's what I think is the relevant part of my urls.py:
这是我认为是我的 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)
采纳答案by Devin
I added the same line in my urls.py and got the same error as you.
我在我的 urls.py 中添加了相同的行并且得到了和你一样的错误。
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The documentation heresays to use settings.STATIC_URLand settings.STATIC_ROOT
此处的文档说要使用settings.STATIC_URL和settings.STATIC_ROOT
I changed it to the documentation version
我将其更改为文档版本
urlpatterns = patterns('',
....urls......
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
and the error went away!
错误消失了!
I checked my settings file and made sure settings.MEDIA_URLand settings.MEDIA_ROOTwere both defined correctly. Later I adjusted urls.py back to using settings.MEDIA_URLand settings.MEDIA_ROOT. Everything worked as expected.
我检查我的设置文件,并确保settings.MEDIA_URL并settings.MEDIA_ROOT双双正确定义。后来我将 urls.py 调整回使用settings.MEDIA_URLand settings.MEDIA_ROOT。一切都按预期进行。
These are the relevant parts of my settings.py file:
这些是我的 settings.py 文件的相关部分:
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/')
I think MEDIA_URLwas set incorrectly
我认为MEDIA_URL设置不正确
回答by Mykel
Just in case you have this issue, ensure you have both MEDIA_URL AND MEDIA_ROOT set.
以防万一您遇到此问题,请确保您同时设置了 MEDIA_URL 和 MEDIA_ROOT。
I was receiving the error
我收到错误
ImproperlyConfigured at / Empty static prefix not permitted
when I only had MEDIA_ROOT set in django 1.11
当我在 Django 1.11 中只设置了 MEDIA_ROOT 时
Alternatively, django project wiki says that it cannot refer to a URL in debug mode: https://docs.djangoproject.com/en/1.11/howto/static-files/#serving-files-uploaded-by-a-user-during-development
或者,django 项目维基说它不能在调试模式下引用 URL:https: //docs.djangoproject.com/en/1.11/howto/static-files/#serving-files-uploaded-by-a-user-开发中
回答by Projesh Bhoumik
You have too check both of the MEDIA_URL and MEDIA_ROOT as well as for static files STATIC_ROOT STATIC_URL are defined correctly.
您也必须检查 MEDIA_URL 和 MEDIA_ROOT 以及静态文件 STATIC_ROOT STATIC_URL 是否正确定义。
Check correct spelling also :)
还要检查拼写是否正确:)
If one of them is miss configured those will cause this error.
如果其中之一未配置,则会导致此错误。
回答by TejasPancholi
I had recently got the same error when working with Django 2.1 The problem was I did not specify explicitly MEDIA_URL = '/media/'in the project settings file. Once I declared the same the error went away.
我最近在使用 Django 2.1 时遇到了同样的错误问题是我没有MEDIA_URL = '/media/'在项目设置文件中明确指定。一旦我声明相同,错误就消失了。
回答by ehacinom
To fix this error, I had to put STATIC_ROOTand STATIC_URLabove the STATICFILES_DIRSdeclaration.
要修正这个错误,我只好把STATIC_ROOT和STATIC_URL上述STATICFILES_DIRS声明。
回答by Antonio José
To resolve the problem, the following statements must be added to the settings.pyfile:
要解决此问题,必须在settings.py文件中添加以下语句:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
回答by Avinash Pandey
Make sure that settings.py has:
确保 settings.py 具有:
# Media
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
Then in the urls.py, try this
然后在 urls.py 中,试试这个
urlpatterns[
...
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
回答by sogu
I am following Django 2.2 & Python | The Ultimate Web Development Bootcampmy issue was that I have forgot to declare these to portfolio-project/portfolio/settings.py/ to the bottom area:
我正在关注Django 2.2 & Python | Ultimate Web Development Bootcamp我的问题是我忘记将这些声明到组合项目/组合/设置.py/ 到底部区域:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/Media/'

