程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django media routing configuration

編輯:Python

Reuse media When saving uploaded pictures , The page does not read pictures , Console display page not found 404, The path is clearly right , Why can't I read the picture ? Because it didn't give media To configure url

step : Just copy mine directly
settings Set up media

MEDIA_URL = '/media/'
# Set the path to upload files
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # Specify the root directory

Folder with the same name as the project urls:

from django.views.static import serve # Upload file processing function
from .settings import MEDIA_ROOT # Import... From configuration MEDIA_ROOT
urlpatterns = [
url(r'^media/(?P<path>.*)$', serve, {"document_root":MEDIA_ROOT})
]

settings:

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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',
# Add this sentence
'django.template.context_processors.media', # take media_url Upload file path register to template
],
},
},
]

And then in the corresponding page {%get_media_prefix%}{ {image.path}} that will do


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved