Django實(shí)現(xiàn)后臺上傳并顯示圖片功能
1.安裝pillow
pip install Pillow
2.創(chuàng)建app
python manage.py startapp upload
3. project設(shè)定
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 settingMEDIA_ROOT = os.path.join(BASE_DIR, ’media’).replace('', '/')MEDIA_URL = ’/media/’
urls.py
from django.contrib import adminfrom django.urls import path,includefrom django.conf.urls.static import staticfrom django.conf import settingsurlpatterns = [ 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 設(shè)定
models.py
from django.db import modelsclass 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 adminfrom .models import *# Register your models here.admin.site.register(User)
urls.py
from django.contrib import adminfrom django.urls import path, register_converter, re_pathfrom . import viewsurlpatterns = [ path(’’, views.index, name=’index’), # 上傳首頁]
views.py
from django.shortcuts import renderfrom .models import Userfrom django.http import HttpResponse# Create your views here.def index(request): users = User.objects.all()return render(request, ’upload/index.html’, locals())
5 . 前臺設(shè)定
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='http://www.gepszalag.com/bcjs/{{ MEDIA_URL }}{{ user.avator }}' alt=''></li> {% endfor %}</ul></body></html>
6. migraiton
python manage.py makemigrationspython manage.py migratepython manage.py createsuperuserpython manage runserver 0.0.0.0:8000
7.進(jìn)行管理后臺上傳user 圖片http://localhost:8000/admin
8.顯示 http://localhost:8000/upload/
Django實(shí)現(xiàn)前臺上傳并顯示圖片功能
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明3. CSS hack用法案例詳解4. PHP設(shè)計(jì)模式中工廠模式深入詳解5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. ASP+ajax實(shí)現(xiàn)頂一下、踩一下同支持與反對的實(shí)現(xiàn)代碼7. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法8. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過程(親測可用)9. asp中response.write("中文")或者js中文亂碼問題10. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析
