1. 程式人生 > >django處理上傳檔案配置

django處理上傳檔案配置

1.sttings中配置 'django.template.context_processors.media'
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', 'django.template.context_processors.media', ], }, }, ]

2.在靜態頁面中設定

{{ MEDIA_URL }}{{ course_org.image }}
{% for course_org in all_org %}
                     <dl class="des difdes">
                    <dt>
                        <a href="org-detail-homepage.html">
                            <img width="200" height="120" class
="scrollLoading" data-url="{{ MEDIA_URL }}{{ course_org.image }}"/> </a> </dt> <dd>

3.在路由設定

 
 
from django.views.static import serve # 處理靜態檔案的
from studyimooc.settings import MEDIA_ROOT
# 配置上傳檔案的函式
url(r'^media/(?P<path>.*)$', serve, {"document_root": MEDIA_ROOT})