V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fanne
V2EX  ›  Django

django models 中的 choice 内容,如何在前端的中以下拉菜单展示出来

  •  
  •   fanne · 2018-08-27 18:51:50 +08:00 · 5120 次点击
    这是一个创建于 2040 天前的主题,其中的信息可能已经有所发展或是发生改变。
    models.py 内容
    class IdcInfo(models.Model):
        virtual_choice = (
            (0, '否'),
            (1, '是'),
        )
        idc_name = models.CharField(max_length=50, verbose_name=('机房名字'), default=u"", null=True, blank=True)
        is_virtual = models.IntegerField(verbose_name=("是否云主机"), choices=virtual_choice, default=0)
    
    views.py 内容
    class IdcAddView(View):
        """
        添加 IDC 信息
        """
        def get(self, request):
            if request.user.is_superuser:
                is_virtual_list = IdcInfo.objects.all()
                print is_virtual_list
                return render(request, "assets/idcadd.html", {is_virtual_list: 'is_virtual_list'})
            else:
                return HttpResponse("用户无权限")
    

    前端 html 内容

     <select class="form-control m-b" name="is_virtual">
                                            {% for  is_virtual_i in is_virtual_list %}
                                        <option>{{ is_virtual_i.get_is_virtual_choices_displa }}</option>
                                            {% endfor %}
                                        <option>option 2</option>
                                    </select>
    

    image

    17 条回复    2018-08-28 09:51:34 +08:00
    versionzhang
        1
    versionzhang  
       2018-08-27 19:17:22 +08:00 via Android
    看下 django-crispy-forms 库
    chengxiao
        2
    chengxiao  
       2018-08-27 19:33:58 +08:00
    is_virtual_i.is_virtual.get_is_virtual_display
    还是 is_virtual_i.get_is_virtual_display 来着,忘了是哪个了 可以试试
    就记得是 obj.get_字段_display
    fanne
        3
    fanne  
    OP
       2018-08-27 21:29:31 +08:00
    @chengxiao #2 今天下午都试过了,不行,应该是其他地方问题,但找不到哪里问题
    chengxiao
        4
    chengxiao  
       2018-08-27 21:54:52 +08:00
    @fanne
    <option>{{ is_virtual_i.get_is_virtual_choices_displa }}</option>
    你这里是不是少了一个 y...
    Faiz555
        5
    Faiz555  
       2018-08-27 22:08:52 +08:00
    首先,我推荐视图直接使用 ListView,简单易用

    ```python
    class IdcAddView(ListView):
    """
    添加 IDC 信息
    """
    model = IdcInfo
    context_object_name = 'is_virtual_list'
    template_name = 'lib/idcadd.html'

    def get_queryset(self):
    return IdcInfo.objects.all()
    ```

    然后,模板部分书写有误,应为:

    ```html
    <select class="form-control m-b" name="is_virtual">
    {% for is_virtual_i in is_virtual_list %}
    <option>{{ is_virtual_i.get_is_virtual_display }}</option>
    {% endfor %}
    <option>option 2</option>
    </select>
    ```

    效果图:

    ![option]( https://a.photo/images/2018/08/27/-2018-08-27-22.07.05.png)
    Faiz555
        6
    Faiz555  
       2018-08-27 22:10:21 +08:00
    @fanne 回复没用成 md 格式,将就看吧
    fanne
        7
    fanne  
    OP
       2018-08-27 22:26:05 +08:00
    @chengxiao #2 不是,复制进来问题,正常写,是带 y 的
    fanne
        8
    fanne  
    OP
       2018-08-27 22:30:48 +08:00
    @Faiz555 #5 我继承的是这个 from django.views.generic.base import View
    后面还有一个
    def post (self, request):
    pass


    能否基于我原本的,帮我定位问题所在么
    Faiz555
        9
    Faiz555  
       2018-08-27 23:02:40 +08:00
    使用内建视图导入这个就行了 `from django.views.generic import ListView`
    视图的 context 写错了,是`return render(request, "lib/idcadd.html", {'is_virtual_list': is_virtual_list})`
    context 类似于字典,前面是 key,后面才是 value
    fanne
        10
    fanne  
    OP
       2018-08-27 23:07:41 +08:00
    @Faiz555 #5 这就尴尬了,用了你的方式也有问题,不知道哪里问题,是不是 models.py 问题的
    Faiz555
        11
    Faiz555  
       2018-08-27 23:09:15 +08:00
    @fanne <option>{{ is_virtual_i.get_is_virtual_display }}</option> 这里你改了吗
    你原来模板里写的含有 choices,这是多余的
    fanne
        12
    fanne  
    OP
       2018-08-27 23:26:06 +08:00
    @Faiz555 #11 改了的,方便加 Q 或者邮箱沟通么,从下午调到现在了

    <option>{{ is_virtual_i.get_is_virtual_display }}</option>

    return render(request, "assets/idcadd.html", {'is_virtual_list': is_virtual_list})

    这两个地方都改了
    fanne
        13
    fanne  
    OP
       2018-08-28 08:57:35 +08:00
    @Faiz555 #11 你 queryset 直接交互查询,能查到 choice 里面的内容么,我好像这么查也查不到
    fanne
        14
    fanne  
    OP
       2018-08-28 09:01:52 +08:00
    @Faiz555 #11
    In [4]: from assets.models import IdcInfo

    In [5]: IdcInfo.objects.all()[0].is_virtual
    ---------------------------------------------------------------------------
    IndexError Traceback (most recent call last)
    <ipython-input-5-73f9e2b4c7bb> in <module>()
    ----> 1 IdcInfo.objects.all()[0].is_virtual

    /Users/apple/OneDrive/Code_7zGame/Envs/tdops/lib/python2.7/site-packages/django/db/models/query.pyc in __getitem__(self, k)
    287 qs = self._clone()
    288 qs.query.set_limits(k, k + 1)
    --> 289 return list(qs)[0]
    290
    291 def __and__(self, other):

    IndexError: list index out of range
    Faiz555
        15
    Faiz555  
       2018-08-28 09:46:45 +08:00
    我能查到,你确认数据是存在的?
    fanne
        16
    fanne  
    OP
       2018-08-28 09:48:43 +08:00
    @Faiz555 #15 models 写完后,makemigrations, migrate 之后,里面表是空的,正常还没插入数据,这张表不就是空的么?
    fanne
        17
    fanne  
    OP
       2018-08-28 09:51:34 +08:00
    @Faiz555 #15 您查出来的信息是怎样的?方便贴一下 models 里的内容么
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3215 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 13:07 · PVG 21:07 · LAX 06:07 · JFK 09:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.