[Django] choices Dictionary defined in models displays values on the page

Source: Internet
Author: User
Problem:

In models. py of Django, we define some choices tuples, which are similar to some dictionary values. Generally, they are drop-down boxes or single-choice boxes. For example, 0 corresponds to male and 1 corresponds to female.

Class area (models. model): area_level = (0, u 'China'), (1, u 'province, municipality '), (2, u 'city, municipality Region '), (3, U' district, county, etc. '),) areaname = models. charfield (max_length = 30, unique = true, verbose_name = 'region name') code = models. charfield (max_length = 20, blank = true, default = "", verbose_name = 'region Code') parentid = models. integerfield (verbose_name = 'parent id', null = true) level = models. integerfield (choices = area_level, verbose_name = 'hierd', null = true)

There is a table on the page to display the fields in the table. If the database stores 0, it is displayed nationwide, and 1 shows the provinces and municipalities are similar:

Name code level operations in upper-level regions nationwide (0) delete Beijing BJ province and municipality (1) Delete nationwide

Is there any way in Django that we can use directly?


Solution:

We can check the first result by Google:

Http://stackoverflow.com/questions/4320679/django-display-choice-value

Here is the answer. One is to use Django's own method, and the other is to use if to judge (this is obviously not liked by everyone)


The Django documentation is here:

Https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.get_FOO_display

On the page, we can directly display the dictionary value as long as we write it like this.

 <td>{{ obj.get_level_display  }}({{ obj.level }})</td>

OBJ. Get _ field name _ display.

To use the framework well, you still need to read more documents.


This article is from "orangleliu notebook "? Blog, reprint please be sure to keep this source http://blog.csdn.net/orangleliu/article/details/40268093

[Django] choices Dictionary defined in models displays values on the page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.