您的位置:首页 > 博客中心 > 互联网 >

django-- 配置media文件

时间:2022-05-05 23:57

设置media static

本文python版本3.6.1,Django版本1.11.1

1、settings.py配置

增加django.template.context_processors.media

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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‘,             ],         },     }, ]

增加MEDIA_URL、MEDIA_ROOT

1 2 3 MEDIA_URL = ‘/media/‘   MEDIA_ROOT = os.path.join(BASE_DIR, ‘media‘)

2、urls.py

1 2 3 4 5 from east_web.settings import MEDIA_ROOT from django.views.static import serve   # 配置上传文件的访问处理函数     url(r‘^media/(?P.*)$‘,  serve, {"document_root": MEDIA_ROOT}),

3、models.py使用

1 image = models.ImageField(max_length=100, upload_to="image/%Y/%m", default=u"image/default.png", verbose_name=u‘头像‘)

 

本类排行

今日推荐

热门手游