人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

django實現(xiàn)圖片上傳數(shù)據(jù)庫并顯示

發(fā)布日期:2022-02-05 18:19 | 文章來源:CSDN

Django文件上傳,供大家參考,具體內(nèi)容如下

需求

1、完成學(xué)生信息注冊操作
2、將學(xué)生信息入庫
3、將上傳文件存放至項目下media文件夾下
4、顯示所有學(xué)生信息

創(chuàng)建模型類

class Student(models.Model):
 sno = models.AutoField(primary_key=True)
 sname = models.CharField(max_length=30)
 photo = models.ImageField(upload_to='imgs')
 <!--內(nèi)部類寫法 數(shù)據(jù)庫中的名字-->
 class Meta:
  db_table = 't_stu'
 def __str__(self):
  return self.sname

settings.py文件中文件上傳相關(guān)設(shè)置

INSTALLED_APPS = [
 ...
 'stu'
]
DATABASES = {
  'default': {
  'ENGINE': 'django.db.backends.mysql',
  'NAME': 'django22',
  'USER': 'root',
  'PASSWORD': '123321',
  'HOST': '127.0.0.1',
  'PORT': '3306',
 }
}
MEDIA_URL = '/media/'
<!--設(shè)置MEDIA_ROOT 默認(rèn)為空  模型類中圖片上傳地址 MEDIA_ROOT+up_load -->
<!--BASE_DIR 為項目錄 -->
MEDIA_ROOT = os.path.join(BASE_DIR,'media')

映射數(shù)據(jù)庫表

#在終端中敲命令
python manage.py makemigrations test
python manage.py migrate

配置URL

主路由

from django.contrib import admin
from django.urls import path, re_path,include
from djurls.settings import MEDIA_ROOT
from stu import urls
from .import views
#配置路由讀取后臺上傳文件
from django.views.static import serve
urlpatterns = [
  path('test/',include('test.urls')),
re_path(r'^media/(?P<path>.*)/$', serve, {"document_root": MEDIA_ROOT}),
#server 視圖函數(shù) 將MEDIA的路徑和正則匹配的模板路徑 顯示圖片

子路由

from django.urls import path
from test import views

urlpatterns = [
 path('test/',views.index.as_view()),
 path('show/',views.show)
]

創(chuàng)建視圖

stu/views.py

import os
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render
from django.views import View
from djurls.settings import BASE_DIR
from test.models import Student
<!--通過as_view處理自動獲取請求方式-->
class index(View):
 def get(self,request):
  return render(request,'load.html')
 def post(self,request):
  name=request.POST.get('sname','')
  photo=request.FILES.get('photo','')
  age=request.POST.get('age','')
  <!--進行校驗 將文件名的后綴字符串分割 判斷-->
  extenedname=photo.name[photo.name.rindex('.')+1:]
  allowedname=['jpg','png']
  if extenedname not in  allowedname:
return Http404()
  stu=Student.objects.create(sname=name,age=20,photo=photo)
  if stu:
return HttpResponse('注冊成功')
  else:
return HttpResponseRedirect('/test/test/')

def show(request):
 stulist=Student.objects.all()
 return render(request,'show.html',{'stulist':stulist})

創(chuàng)建模板

templates/index.html 注冊界面

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
 <form action="/test/test/"  method="post" enctype="multipart/form-data">
  {% csrf_token %}
  <p>姓名<input type="text" name="sname"></p>
  <p>年齡 <input type="number" name="age"></p>
  <p>照片 <input type="file" name="photo"></p>
  <input type="submit" value="注冊">
 </form>
</body>
</html>

show.html 顯示數(shù)據(jù) 加載圖片

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
 <table border="1px solid black" cellspacing="0px" width="500px" align="center">
  <tr height="100px" align="center">
<td >編號</td>
<td >姓名 </td>
<td >年齡</td>
<td >頭像</td>
  </tr>
  {% for stu in stulist %}
  <tr height="100px" align="center">
<td >{{ forloop.counter }} </td>
<td >{{ stu.sname }}</td>
<td >{{ stu.age }}</td>
<td ><img src="/media/{{ stu.photo }}" alt=""></td>
<!--讀取photo的路徑 在主路由訪問 通過server處理并顯示-->
  </tr>
  {% endfor %}
 </table>
</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。

國外穩(wěn)定服務(wù)器

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部