成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

Django實(shí)現(xiàn)從數(shù)據(jù)庫中獲取到的數(shù)據(jù)轉(zhuǎn)換為dict

瀏覽:79日期:2024-10-18 16:34:55

這種方式只能應(yīng)用于從數(shù)據(jù)庫中獲取到的單條數(shù)據(jù),例如models.Users.objects.get()獲取到的數(shù)據(jù)

from django.forms.models import model_to_dictclass Index(VIew): def get(self, request): userObj = models.Users.objects.get(id = 1) userDict = model_to_dict(userObj) print(userDict) return HttpResponse(’yes’)

重點(diǎn)是導(dǎo)入的model_to_dict方法

補(bǔ)充知識:django自定義標(biāo)簽使用,Bytes/KB/MB/GB相互轉(zhuǎn)換

目錄結(jié)構(gòu)

templatetags--mytags.pyviews.py

后端代碼 mytags.py

from django import templateregister = template.Library()#bytes單位轉(zhuǎn)換@register.simple_tag()def bytes_convert(num): if not num: return ’’ elif num < 1024: return str(num) + ’ B’ elif 1024 <= num < 1024*1024: return str(round(num/1024,2)) + ’ KB’ elif 1024*1024 <= num < 1024*1024*1024: return str(round(num/(1024*1024),2)) + ’ MB’ else: return str(round(num/(1024*1024*1024),2)) + ’ GB’

前端代碼

{% load mytags %} <--??胱遠(yuǎn)?x?嘶`--><td>{% bytes_convert i.bytes %}</td> <--使用?嘶`-->

以上這篇Django實(shí)現(xiàn)從數(shù)據(jù)庫中獲取到的數(shù)據(jù)轉(zhuǎn)換為dict就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Django
相關(guān)文章: