Django实现后台上传并显示图片功能

所属分类: 脚本专栏 / python 阅读数: 669
收藏 0 赞 0 分享

1.安装pillow

pip install Pillow

2.创建app

python manage.py startapp upload

3. project设定

settings.py

INSTALLED_APPS = [
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'upload.apps.MyuploadConfig', #add this
]

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' #add this
   ],
  },
 },
]

#picture path setting
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace("\\", "/")
MEDIA_URL = '/media/'

urls.py

from django.contrib import admin
from django.urls import path,include
from django.conf.urls.static import static
from django.conf import settings


urlpatterns = [
 path('admin/', admin.site.urls),
 path('', views.index),
 path('upload/', include(('myupload.urls', 'myupload'), namespace='myupload')), # add uppoad urls
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)      #add image path

4. app 设定

models.py

from django.db import models

class User(models.Model):
 name = models.CharField(verbose_name='姓名', max_length=10)
 avator = models.ImageField(verbose_name='头像', upload_to='upload/%Y/%m/%d') 

admin.py

from django.contrib import admin
from .models import *

# Register your models here.
admin.site.register(User)

urls.py

from django.contrib import admin
from django.urls import path, register_converter, re_path
from . import views

urlpatterns = [
 path('', views.index, name='index'), # 上传首页
]

views.py

from django.shortcuts import render
from .models import User
from django.http import HttpResponse

# Create your views here.
def index(request):
 users = User.objects.all()return render(request, 'upload/index.html', locals())

5 . 前台设定

project 目录下 templates/upload/index.html

----------------------------------------------------------------------------------------

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
<ul>
 {% for user in users%}
  <li>{{ user.name }}</li>
  <li><img src="{{ MEDIA_URL }}{{ user.avator }}" alt=""></li>
 {% endfor %}
</ul>
</body>
</html>

6. migraiton

python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage runserver 0.0.0.0:8000

7.进行管理后台上传user 图片http://localhost:8000/admin

8.显示 http://localhost:8000/upload/

Django实现前台上传并显示图片功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

python中seaborn包常用图形使用详解

今天小编就为大家分享一篇python中seaborn包常用图形使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

numpy:找到指定元素的索引示例

今天小编就为大家分享一篇numpy:找到指定元素的索引示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python实现图片上添加图片

这篇文章主要为大家详细介绍了python实现图片上添加图片,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

从numpy数组中取出满足条件的元素示例

今天小编就为大家分享一篇从numpy数组中取出满足条件的元素示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python实现图片添加文字

这篇文章主要为大家详细介绍了Python实现图片添加文字,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

python实现在多维数组中挑选符合条件的全部元素

今天小编就为大家分享一篇python实现在多维数组中挑选符合条件的全部元素,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python如何使用BeautifulSoup爬取网页信息

这篇文章主要介绍了Python如何使用BeautifulSoup爬取网页信息,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

浅谈python已知元素,获取元素索引(numpy,pandas)

今天小编就为大家分享一篇浅谈python已知元素,获取元素索引(numpy,pandas),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

numpy ndarray 按条件筛选数组,关联筛选的例子

今天小编就为大家分享一篇numpy ndarray 按条件筛选数组,关联筛选的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python pickle模块实现对象序列化

这篇文章主要介绍了Python pickle模块实现对象序列化,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多