Django如何在模板中调用选择字段

时间:2017-04-18 22:29:24

标签: django django-models django-templates

我有一个有选择的模型字段。像这样:

CHAIN_CHOICES = (
        ('P','Public'),
        ('N','Private')
        )
        chain = models.CharField(max_length=1, choices=CHAIN_CHOICES, verbose_name=u"Chain")

在我的模板中,我想称之为simillary:

<li><i class="glyphicon glyphicon-tags"></i> <span>{{mymodel.chain}}</span></li>

问题是......这是在呼唤N&#39; N&#39;或者&#39; P&#39;我想打电话给公众&#39; - &#39;私人&#39;值。 任何暗示都会很好。

此致

J.M。

2 个答案:

答案 0 :(得分:1)

猜猜我找到了答案:

{{ mymodel.get_chain_display }}

除非有不同的方式??

答案 1 :(得分:0)

@JanMejor
我想从模板打电话到&#34;选择&#34;也没有重复。以下是您采用的不同方式:

$tdno = '%%';
$titleno = '%%';
$owner = '%%';
$transaction = '%%';
$location = '%%';
$sql = "SELECT 
  history_table.`history_id`,
  history_table.`TDNO` AS 'TAX DECLARATION NO',
  history_table.`titleNO` AS 'TITLE NO',
  history_table.`lotNO` AS 'LOT NO',
  history_table.`area` AS 'AREA',
  history_table.`encumbrances` AS 'ENCUMBRANCES',
  FORMAT(history_table.`assess_value`, 2) AS 'ASSESS VALUE',
  history_table.`EFF`,
  history_table.`memo_id`,
  memoranda.`memo_string` AS MEMORANDA, 
  TD_ownersinfo.`owner`,
  TD_ownersinfo.`location`,
  TD_ownersinfo.`transaction`
FROM
  history_table 
  INNER JOIN memoranda 
    ON memoranda.memo_id = history_table.memo_id 
  LEFT JOIN td_ownersinfo 
    ON history_table.TDNO = td_ownersinfo.TD_NO
WHERE history_table.TDNO LIKE '$tdno'
  AND history_table.titleNO LIKE '$titleno'
  AND (td_ownersinfo.owner LIKE '$owner' OR '$owner'='%%' AND td_ownersinfo.TD_NO IS NULL)
  AND (td_ownersinfo.transaction LIKE '$transaction' OR '$transaction'='%%' AND td_ownersinfo.TD_NO IS NULL) 
  AND (td_ownersinfo.location LIKE '$location' OR '$location'='%%' AND td_ownersinfo.TD_NO IS NULL)
  OR 
  td_ownersinfo.TD_NO IS NULL 
  AND '$tdno$titleno$owner$transaction$location' = '%%%%%%%%%%'
ORDER BY history_table.history_id";
// use the appropriate db command here...
$result = $conn->query($sql);

如果你有几个帖子具有相同的&#34;选择值&#34;

,它仍会显示重复项