第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

以更具可讀性的格式顯示電話號(hào)碼

以更具可讀性的格式顯示電話號(hào)碼

猛跑小豬 2022-10-05 09:22:10
請(qǐng)告訴我如何正確解決以下問(wèn)題數(shù)據(jù)庫(kù)中有一個(gè)電話號(hào)碼。在網(wǎng)站上,它以與數(shù)據(jù)庫(kù)中記錄的相同形式顯示+1234567890我需要這個(gè)數(shù)字看起來(lái)像這樣,在網(wǎng)站頁(yè)面上,無(wú)需在數(shù)據(jù)庫(kù)中進(jìn)行修改+(123) 456-7890views.pydef Data(request):     card = Cards.objects.filter(is_published=True)         context = {'card': card}                 return render(request, 'templates/cards.html', context)models.pyphone = models.CharField(max_length=12, null=True)卡片.html{{ card.phone }}謝謝!
查看完整描述

2 回答

?
大話西游666

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊

Django不會(huì)自己格式化數(shù)字。但是,您可以構(gòu)建自己的函數(shù)來(lái)執(zhí)行此操作。考慮使用(鏈接)。在 models.py 中為此創(chuàng)建另一個(gè)函數(shù):python-phonenumbers


import phonenumbers


class Cards:

    phone = models.CharField(max_length=12, null=True)

    .....


    def formatted_phone_number(self, country=None):

        rtval = phonenumbers.parse(self.phone, country)

        return rtval

然后在您的:template


{{ card.formatted_phone_number }}

注意:不能直接從模板傳遞參數(shù)。因此,該函數(shù)必須事先知道您將使用哪種國(guó)家/地區(qū)格式。


查看完整回答
反對(duì) 回復(fù) 2022-10-05
?
ABOUTYOU

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以編寫此幫助程序函數(shù):


def insert_str(string, str_to_insert, index):

    return string[:index] + str_to_insert + string[index:]

,然后使用它來(lái)在需要時(shí)插入字符。如果括號(hào)的位置是固定的(每個(gè)電話號(hào)碼都相同,如您提供的示例所示),那么您可以簡(jiǎn)單地執(zhí)行以下操作:


phone = insert_str(phone, '(', 1)

phone = insert_str(phone, '(', 5) # there's already an extra '('

phone = insert_str(phone, ' ', 6)

phone = insert_str(phone, '-', 10)


查看完整回答
反對(duì) 回復(fù) 2022-10-05
  • 2 回答
  • 0 關(guān)注
  • 100 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)