Variable as the key value of the Django dictionary in HTML

Source: Internet
Author: User

If the dictionary is dic={' name ': Barbie, ' age ': 20}, the Dic.name in HTML is barbie,dic.age to 20.

But if the dictionary is dic={' Barbie ': 1, ' Roger ': 2, ' Kitty ': 3}, to use the variable name in HTML to represent the name, you cannot get the corresponding value directly with Dic.name.

Official Document: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

Add the following statement in the PY:

From django.template.defaulttags Import Register
@register. Filter
def get_item (dictionary, key):
return Dictionary.get (Key)

In HTML, get the following values:

{{Dic|get_item:mainuser}}

Variable as the key value of the Django dictionary in HTML

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.