Django實(shí)現(xiàn)后臺(tái)上傳并顯示圖片功能
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 . 前臺(tái)設(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.進(jìn)行管理后臺(tái)上傳user 圖片http://localhost:8000/admin
8.顯示 http://localhost:8000/upload/
Django實(shí)現(xiàn)前臺(tái)上傳并顯示圖片功能
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Django自定義圖片和文件上傳路徑(upload_to)的2種方式
- Django實(shí)現(xiàn)前臺(tái)上傳并顯示圖片功能
- Django實(shí)現(xiàn)圖片上傳功能步驟解析
- Django {{ MEDIA_URL }}無法顯示圖片的解決方式
- Django 實(shí)現(xiàn)將圖片轉(zhuǎn)為Base64,然后使用json傳輸
- django 讀取圖片到頁面實(shí)例
- django中的圖片驗(yàn)證碼功能
- django項(xiàng)目登錄中使用圖片驗(yàn)證碼的實(shí)現(xiàn)方法
- Django 實(shí)現(xiàn)前端圖片壓縮功能的方法
- Django 實(shí)現(xiàn)圖片上傳和下載功能
相關(guān)文章
一文向您詳細(xì)介紹指令 python -m pip install的用法和功能
通過本文的介紹,我們詳細(xì)了解了python -m pip install命令的用法和功能,從基本用法到安裝特定版本的包、從其他源安裝包、升級(jí)和卸載包,再到使用requirements.txt管理依賴,我們逐步深入了解了pip的強(qiáng)大功能,感興趣的朋友跟隨小編一起看看吧2024-07-07
python?動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)模塊熱更新的方法
這篇文章主要介紹了python?動(dòng)態(tài)導(dǎo)入模塊,實(shí)現(xiàn)模塊熱更新,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
Python confluent kafka客戶端配置kerberos認(rèn)證流程詳解
這篇文章主要介紹了Python confluent kafka客戶端配置kerberos認(rèn)證流程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Python+Pytest實(shí)現(xiàn)壓力測試詳解
在現(xiàn)代Web應(yīng)用程序中,性能是至關(guān)重要的。為了確保應(yīng)用程序能夠在高負(fù)載下正常運(yùn)行,我們需要進(jìn)行性能測試。本文就來用Pytest進(jìn)行壓力測試,希望對大家有所幫助2023-03-03
python中函數(shù)傳參的幾種實(shí)現(xiàn)方式
這篇文章主要介紹了python中函數(shù)傳參的幾種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
python高手之路python處理excel文件(方法匯總)
用python來自動(dòng)生成excel數(shù)據(jù)文件。python處理excel文件主要是第三方模塊庫xlrd、xlwt、xluntils和pyExcelerator,除此之外,python處理excel還可以用win32com和openpyxl模塊2016-01-01

