0

我想创建两个字段:单选按钮和下拉单选按钮将填充给定的选项,下拉菜单将从数据库中提取。卡在显示收音机和获取价值上

我应该使用modelform还是form?当单选按钮选择与下拉值一起关闭时,我想将数据存储在数据库中,并希望使用该值来显示案例类型。

模型.py
class Selections(models.Model):

    roletype = models.CharField(max_length=8, default=None)
    LOCATIONS = (('DC1','DC1'), ('DC2', 'DC2'), ('DC3', 'DC3'))
    location = models.CharField(max_length=4, choices=LOCATIONS)

    def __str__(self):
        return self.roletype
表格.py
from django import forms
from .models import Selections
class SelectionsForm(forms.ModelForm):
    ROLES = (('1','Type1'),('2','Type2'),('3', 'Type3'))
    roletype = forms.ChoiceField(choices=ROLES, widget=forms.RadioSelect)

    class Meta:
        model = Selections
        fields = ['roletype','location']
视图.py
## VIEW: Populate dropdown to select Data center
def get_role_dc_data(request, *args, **kwargs):

    location = Selections.objects.get(id=0) # List of objects
    roletype = Selections.objects.get(id=1) # List of objects

    context = { 'location': location, 'roletype': roletype }

    return render(request, 'selections/view.html', context)
视图.html
<form method="post" action=/viewselections/> {% csrf_token %}
    <table class="table">
      <tr>
      <th>
          {% for radio_input in context.roletype %}
          <input type="radio" name="choice" id="{{ radio_input.id }}" value="{{ radio_input.id }}">
          {% endfor %}
        </th>
        <th>
          <div class="fieldWrapper" align="center" id="mainselection">
          <select name="location">
            <option selected="selected">Select an the site</option>
            {% for instance in context.location %}
            <option >{{ instance.location }}</option>
            {% endfor %}
          </select>
          </div>
        </th>
      </tr>
    </table>
</form>

无法获取值。不确定这是否正确同样一旦用户选择了选项,如何显示返回数据并存储在数据库中。

4

0 回答 0