Django實現(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 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 設(shè)定
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 . 前臺設(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="{{ 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/
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python通過getattr函數(shù)獲取對象的屬性值
這篇文章主要介紹了Python通過getattr函數(shù)獲取對象的屬性值,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10Python連接MySQL數(shù)據(jù)庫的簡單便捷方法
在數(shù)據(jù)分析過程中往往要操作較大的數(shù)據(jù)集,這就需要連接數(shù)據(jù)庫進行操作,下面這篇文章主要給大家介紹了關(guān)于Python連接MySQL數(shù)據(jù)庫的簡單便捷方法,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-04-04Pytorch 解決自定義子Module .cuda() tensor失敗的問題
這篇文章主要介紹了Pytorch 解決自定義子Module .cuda() tensor失敗的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python二叉樹的遍歷操作示例【前序遍歷,中序遍歷,后序遍歷,層序遍歷】
這篇文章主要介紹了Python二叉樹的遍歷操作,結(jié)合實例形式分析了Python針對二叉樹的前序遍歷,中序遍歷,后序遍歷,層序遍歷等相關(guān)操作實現(xiàn)技巧,需要的朋友可以參考下2018-12-12詳解如何在PyQt5中實現(xiàn)平滑滾動的QScrollArea
Qt 自帶的 QScrollArea 滾動時只能在兩個像素節(jié)點之間跳變,看起來很突兀。所以本文將通過定時器,重寫 wheelEvent() 來實現(xiàn)平滑滾動,需要的可以參考一下2023-01-01python輸出當前目錄下index.html文件路徑的方法
這篇文章主要介紹了python輸出當前目錄下index.html文件路徑的方法,涉及Python操作目錄的相關(guān)技巧,需要的朋友可以參考下2015-04-04