欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2021年08月24日 16:40:37   作者:浪蕩之徒  
這篇文章主要為大家詳細(xì)介紹了django實現(xiàn)圖片上傳數(shù)據(jù)庫并顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

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','')
        <!--進(jìn)行校驗 將文件名的后綴字符串分割 判斷-->
        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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Django瀑布流的實現(xiàn)示例

    Django瀑布流的實現(xiàn)示例

    在瀏覽一些網(wǎng)站時,經(jīng)常會看到類似于這種滿屏都是圖片,本文主要介紹了Django瀑布流的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2023-03-03
  • 一文搞定FastAPI中的查詢參數(shù)

    一文搞定FastAPI中的查詢參數(shù)

    FastAPI中最核心的之一就是路徑參數(shù),所以這篇文章小編主要來和大家介紹一下FastAPI查詢參數(shù)的作用以及基本使用,有需要的小伙伴可以參考下
    2024-03-03
  • 如何利用Python和OpenCV對圖像進(jìn)行加水印詳解

    如何利用Python和OpenCV對圖像進(jìn)行加水印詳解

    Python使用opencv是因為覺得它足夠強(qiáng)大,很多圖像處理這塊都是用的它,最近就用opencv添加個水印,這篇文章主要給大家介紹了關(guān)于如何利用Python和OpenCV對圖像進(jìn)行加水印的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • python中sympy庫求常微分方程的用法

    python中sympy庫求常微分方程的用法

    這篇文章主要介紹了python中sympy庫求常微分方程的用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Python max內(nèi)置函數(shù)詳細(xì)介紹

    Python max內(nèi)置函數(shù)詳細(xì)介紹

    這篇文章主要介紹了Python MAX內(nèi)置函數(shù)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • Python PyCryptodome庫介紹與實例教程

    Python PyCryptodome庫介紹與實例教程

    PyCryptodome提供了豐富的加密功能,可以滿足多種安全需求,本文介紹了幾個常見的使用場景,包括對稱加密、非對稱加密、哈希函數(shù)和消息認(rèn)證碼,感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • python中24小時制轉(zhuǎn)換為12小時制的方法

    python中24小時制轉(zhuǎn)換為12小時制的方法

    最近需要實現(xiàn)一個需求,求用戶輸入24小時制的時間,然后顯示12小時制的時間。具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • 基于python神經(jīng)卷積網(wǎng)絡(luò)的人臉識別

    基于python神經(jīng)卷積網(wǎng)絡(luò)的人臉識別

    這篇文章主要為大家詳細(xì)介紹了基于python神經(jīng)卷積網(wǎng)絡(luò)的人臉識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • hmac模塊生成加入了密鑰的消息摘要詳解

    hmac模塊生成加入了密鑰的消息摘要詳解

    這篇文章主要介紹了hmac模塊生成加入了密鑰的消息摘要詳解,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • python實現(xiàn)桌面托盤氣泡提示

    python實現(xiàn)桌面托盤氣泡提示

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)桌面托盤氣泡提示,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07

最新評論