HTML tags for choicefield in Django

rod*_*ing 4 html python django

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>
Run Code Online (Sandbox Code Playgroud)

Help would be greatly appreciated

models.py

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

view.py

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

Tho*_*mas 6

ModelForms是你的朋友.

models.py

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

forms.py

from django.forms import ModelForm
from . import models

class PresetListForm(ModelForm):
    class Meta:
        model = models.PresetList
Run Code Online (Sandbox Code Playgroud)

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,
    })
Run Code Online (Sandbox Code Playgroud)

services.html

<form method=POST action="/somewhere">
    {{ preset_form.as_p }}
</form>
Run Code Online (Sandbox Code Playgroud)