Django中choicefield的HTML标签 [英] HTML tags for choicefield in Django

查看:155
本文介绍了Django中choicefield的HTML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在任何地方找到如何在Django中选择HTML字段的标签。我发现单选按钮和其他高级选项字段,但基本上没有用Django下拉HTML标签。我有 models.py view.py 设置传球 list1 html 页面,但无法显示任何内容,除了

I cant seem to find ANYWHERE on how to do choicefield HTML tags in Django. I found radio buttons and other advance choice fields, but nothing on basic drop down HTML tags with Django. I have models.py and view.py set up passing list1 to the html pages, but cant seem to make it display anything except

<select style="width:300px">
  {% for choice in list1.VIEWS %}
  <option>{{choice}}</option>
  {{choice}}
  {% endfor %}
</select>

帮助将不胜感激

模型.py



Help would be greatly appreciated

class preset_list(models.Model):
    VIEWS = (
        ('1', 'X'),
        ('2', 'Y'),
    )
    query_choice = forms.ChoiceField(choices=VIEWS)



view.py



view.py

list1 = models.preset_list()
return render_to_response('services.html', 
         {'array':json.dumps(data, cls=SpecialEncoder),
         'list1':list1},
                          )


推荐答案

ModelForms是您的朋友。

ModelForms are your friend here.

class PresetList(models.Model):
    VIEWS = (
        ('1', 'X'),
        ('2', 'Y'),
    )
    query_choice = forms.ChoiceField(choices=VIEWS)



forms.py



forms.py

from django.forms import ModelForm
from . import models

class PresetListForm(ModelForm):
    class Meta:
        model = models.PresetList



view.py



view.py

from . import forms

def my_view(request):

    preset_form = forms.PresetListForm()

    return render_to_response('services.html', {
        'array': json.dumps(data, cls=SpecialEncoder),
        'preset_form': preset_form,
    })



services.html



services.html

<form method=POST action="/somewhere">
    {{ preset_form.as_p }}
</form>



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆