Django 3.0 MEDIA_ROOT 和 MEDIA_URL 引发 ImproperlyConfigured 异常
Django 3.0 MEDIA_ROOT and MEDIA_URL raise ImproperlyConfigured exception
我正在开发一个 Django 应用程序,用户有个人资料,他们可以上传个人资料图片,图片将存储在 media/profile_pics/ 中,媒体位于我的根项目目录中。我遵循了 Django 文档 [1]:https://docs.djangoproject.com/en/3.0/howto/static-files/#serving-files-uploaded-by-a-user-during-development
在开发过程中提供上传的媒体文件,但我不断收到一个异常:
`
Exception in thread django-main-thread:
File "/Users/OpenMindes/Dev/web/django/mylatestprotfolio-django/src/portfolio/urls.py", line 27, in <module>
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
File "/Users/OpenMindes/Dev/web/django/mylatestprotfolio-django/lib/python3.8/site-packages/django/conf/urls/static.py", line 22, in static
raise ImproperlyConfigured("Empty static prefix not permitted")
django.core.exceptions.ImproperlyConfigured: Empty static prefix not permitted
My project's settings file looks like that (I hided the secret key):
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#sekret key : i hide it
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#own apps
'my_portfolio.apps.MyPortfolioConfig',
'blog.apps.BlogConfig',
'crud.apps.CrudConfig',
'users.apps.UsersConfig',
#third party
'widget_tweaks',
'crispy_forms',
'django_cleanup',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'portfolio.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'portfolio.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEIDA_URL = '/media/'
#tell our crispy form what bootstrap version to use
CRISPY_TEMPLATE_PACK = 'bootstrap4'
#redirect the user to the home page when successfully loged in
LOGIN_REDIRECT_URL = 'blog-home'
LOGIN_URL = 'login'
And my project urls.py file looks like this:
from django.contrib import admin
from django.urls import path, include
# Import the users views
from users import views as user_views
from django.contrib.auth import views as auth_views
# To serve files during dev
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('',include('my_portfolio.urls')),
path('blog/',include('blog.urls')),
path('crud/',include('crud.urls')),
path('register/', user_views.register, name='user-register'),
#login and logout views
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
#profiles
path('profile/', user_views.profile, name='profile'),
path('testing/', user_views.testingForm, name='testing'),
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
`
我查看了该平台提供的许多解决方案,但 none 似乎有效
你打错了:
MEIDA_URL
应该是:
MEDIA_URL
我正在开发一个 Django 应用程序,用户有个人资料,他们可以上传个人资料图片,图片将存储在 media/profile_pics/ 中,媒体位于我的根项目目录中。我遵循了 Django 文档 [1]:https://docs.djangoproject.com/en/3.0/howto/static-files/#serving-files-uploaded-by-a-user-during-development 在开发过程中提供上传的媒体文件,但我不断收到一个异常:
`
Exception in thread django-main-thread:
File "/Users/OpenMindes/Dev/web/django/mylatestprotfolio-django/src/portfolio/urls.py", line 27, in <module>
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
File "/Users/OpenMindes/Dev/web/django/mylatestprotfolio-django/lib/python3.8/site-packages/django/conf/urls/static.py", line 22, in static
raise ImproperlyConfigured("Empty static prefix not permitted")
django.core.exceptions.ImproperlyConfigured: Empty static prefix not permitted
My project's settings file looks like that (I hided the secret key):
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#sekret key : i hide it
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#own apps
'my_portfolio.apps.MyPortfolioConfig',
'blog.apps.BlogConfig',
'crud.apps.CrudConfig',
'users.apps.UsersConfig',
#third party
'widget_tweaks',
'crispy_forms',
'django_cleanup',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'portfolio.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'portfolio.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEIDA_URL = '/media/'
#tell our crispy form what bootstrap version to use
CRISPY_TEMPLATE_PACK = 'bootstrap4'
#redirect the user to the home page when successfully loged in
LOGIN_REDIRECT_URL = 'blog-home'
LOGIN_URL = 'login'
And my project urls.py file looks like this:
from django.contrib import admin
from django.urls import path, include
# Import the users views
from users import views as user_views
from django.contrib.auth import views as auth_views
# To serve files during dev
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('',include('my_portfolio.urls')),
path('blog/',include('blog.urls')),
path('crud/',include('crud.urls')),
path('register/', user_views.register, name='user-register'),
#login and logout views
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
#profiles
path('profile/', user_views.profile, name='profile'),
path('testing/', user_views.testingForm, name='testing'),
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
` 我查看了该平台提供的许多解决方案,但 none 似乎有效
你打错了:
MEIDA_URL
应该是:
MEDIA_URL