1、使用ImgFiled
class ImageField
(upload_to=None, height_field=None, width_field=None, max_length=100, **options)[source]¶ 返回资源
必须在setting.py 中设置MEDIA
setting.py
MEDIA_URL = '/meta/' MEDIA_ROOT = os.path.join(BASE_DIR,'meta')
models.py
cover_map = models.ImageField(verbose_name='缩列图',upload_to='article/%Y/%m/%d',default='1.png')
upload_to 上传的位置 最后会上传到 /meta/article/年/月/日/**.jpg
如果要显示需要在urls.py设置
from django.contrib import admin from django.urls import path from django.urls import include from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path('admin/', admin.site.urls), path('polls/', include('polls.urls')), path('download/', include('download.urls')), path('article/', include('article.urls')), ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)