显示表单内容而不是表单对象名称

Displaying form content instead of form object name

我是 Django 的新手,我正在尝试在模板中创建表单。我想我已经做到了这一点,但是我遗漏了一块。这是我目前看到的:

我想我的表单 class 中缺少应该显示选项的内容。

forms.py

from django import forms

class TestForm(forms.Form):
    one = forms.ChoiceField(choices=('HDFS', 'HDFS'), widget=forms.RadioSelect())
    two = forms.ChoiceField(choices=('HIVE', 'HIVE'), widget=forms.RadioSelect())
    three = forms.ChoiceField(choices=('BOTH', 'Both of HDFS and HIVE'), widget=forms.RadioSelect())
    beatle = [one, two, three]
    event_textarea = forms.Textarea(attrs={'rows': '8', 'class': 'form-control', 'placeholder': 'Events...', 'id': 'event_textarea'})

views.py

def home(request):
    if request == 'POST':
        # create a form instane and populate it with data from the request
        form = TestForm(request.POST)
        if form.is_valid():
            # process the data in form.cleaned_data as required
            form.cleaned_data()
            # redirect to a new URL:
            return HttpResponseRedirect('/test/')
    # if a GET (or any other method) we'll create a blank form
    else:
        form = TestForm()
    return render(request, 'home/home_page.html', {'form': form})

模板:

{% extends 'index/index.html' %}

{% load staticfiles %}
{% block head %}
  <script type="text/javascript" src="{{ STATIC_URL }}home/js/home.js" async></script>
  <link href="{{ STATIC_URL }}home/css/home.css" rel="stylesheet">
{% endblock head %}

{% block content %}

  <div>Welcome to Trinity E2E testing</div>

  <form id="test-form" action="/test/" method="post"> {# pass data to /test/ URL #}
    {% csrf_token %}
    {% for radio in form.beatle %}
    <div class="btn btn-default btn-lg">
      {{ radio }}
    </div>
    {% endfor %}
    {{ form.event_textarea }}

    <input id="submit-test" type="submit" class="btn btn-default btn-lg" value="Submit">
  </form>

{% endblock content %}

beatle 列表包含对 class 属性的引用,而不是实例属性。

如何使它成为 return 实例属性(表单字段)的实例方法:

def beatle(self):
    return [self.one, self.two, self.three]

更新

要正确return绑定字段:

def beatle(self):
    return [self['one'], self['two'], self['three']]

def beatle(self):
    return [self[name] for name in ['one', 'two', 'three']]