• Models_Class 有choice,如何显示其中文


    方式二:用模板语言

    - get_server_status_id_display( );  #局限性:条件必须是对象QuerySet[对象,对象]

    代码:

    def test(request):
        """
        模板语言显示 choice
        :param request:
        :return:
        """
    
        server_list = models.Server.objects.all()
        for row in server_list:
            print(row.id,row.hostname,
                  "===",
                  row.server_status_id,row.get_server_status_id_display() )
        return render(request, 'test.html', {'server_list': server_list})
    views.py
    <ul>
        {% for row in server_list %}
            <li>{{ row.hostname }}---{{ row.get_server_status_id_display }}</li>
        {% endfor %}
    
    </ul>
    test.html

    -yield实现

    代码:

    def xx(data_list):
        for row in data_list:
            for item in models.Server.server_status_choices:
                if item[0] == row['server_status_id']:
                    row['server_status_id_name'] = item[1]
                    break
                print('***',item)
            yield row
    
    
    def test(request):
        """
        模板语言显示 choice
        :param request:
        :return:
        """
    
        data_list =models.Server.objects.all().values('hostname','server_status_id')
        print(data_list)
    
        return render(request,'test.html',{'server_list':xx(data_list)})  #注意xx()
    View Code
    <ul>
    
        {% for row in server_list %}
            <li>{{ row.hostname }}---{{ row.server_status_id_name }}</li>
        {% endfor %}
    
    </ul>
    test.html

  • 相关阅读:
    MRF能量优化
    Django:model中的ForeignKey理解
    Django:常见的orm操作
    Django:在模板中获取当前url信息
    Django:haystack全文检索详细教程
    Django:全文检索功能可参考博客
    看电影学英语
    Markdown中怎么上传图片
    Mosquitto的安装、配置、测试
    Django:评论文章后局部刷新评论区
  • 原文地址:https://www.cnblogs.com/zhaochangbo/p/7658956.html
Copyright © 2020-2023  润新知