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="/UploadFiles/2021-04-08/{{ MEDIA_URL }}{{ user.avator }}">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实现前台上传并显示图片功能
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月18日
2024年11月18日
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】