【问题标题】:Use buttons (not radiobuttons) for Choicefield in Django?在 Django 中为 Choicefield 使用按钮(不是单选按钮)?
【发布时间】:2020-08-17 02:16:12
【问题描述】:

我有一个 Django 表单:

class Form0(forms.Form):
    # Do you agree to the terms?
    CHOICES = [ (1,'Yes'), (0, 'No')]
    response = forms.ChoiceField(widget=forms.RadioSelect,choices=CHOICES)

我的看法:

def home(request):
    '''
    entry
    '''
    if request.method == 'POST':
        form = forms.Form0(request.POST)
        if form.is_valid():
            assesment, created = models.Assessment.objects.get_or_create(
                session_id=request.session.session_key,
                defaults={'data': {}})
            assesment.data.update({'Terms': form.cleaned_data['response']})
            assesment.save()
            print(form.cleaned_data)
            if form.cleaned_data['response'] == '1':
                return redirect(reverse("question1"))
            else:
                return redirect("conclusion")

            resp =  HttpResponse('ok')
            return resp
    else:
        form = forms.Form0()

    resp = render(request, 'coreapp/index.html', {'form': form})
    if not request.session.session_key:
        request.session.save()
    return resp

它正在我的 HTML 模板中呈现。我制作了一个脚本,可以在单击完美运行的单选按钮时提交表单:

<form method="POST">
  {{ form.as_table }} {% csrf_token %}
  <script>
    $("input[type=radio]").on("change", function () {
      $(this).closest("form").submit();
    });
  </script>
</form>

看起来像这样:

问题

是否可以将“是”和“否”单选按钮显示为普通按钮?例如。使用引导按钮,例如:

<button class="btn btn-primary" type="submit">Yes</button>
<button class="btn btn-primary" type="submit">No</button>

【问题讨论】:

    标签: html css django twitter-bootstrap django-forms


    【解决方案1】:

    我设法通过在我的模板中创建一个 for 循环并向新的“myradio”类添加样式来做到这一点:

    <form method="POST">
      {% csrf_token %}
        {% if form.response %} 
          {% for radio in form.response %}
            <div class="myradio">
              {{ radio }}
            </div>
          {% endfor %}
    
      <script>
        $("input[type=radio]").on("change", function () {
          $(this).closest("form").submit();
        });
      </script>
    </form>
    

    【讨论】:

      【解决方案2】:

      是的,您可以使用引导程序Checkbox and Radio Buttons

      https://getbootstrap.com/docs/4.0/components/buttons/#checkbox-and-radio-buttons

      $('input[type=radio]').on('change', function (e) {
        $(this).closest('form').submit()
      })
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
      
      <form>
        <div class="btn-group btn-group-toggle" data-toggle="buttons">
          <label class="btn btn-primary active">
            <input type="radio" name="options" id="option1" autocomplete="off" checked> Yes
          </label>
          <label class="btn btn-primary">
            <input type="radio" name="options" id="option2" autocomplete="off"> No
          </label>
        </div>
      
        <br />
        <br />
      
        <div class="btn-group-toggle" data-toggle="buttons">
          <label class="btn btn-primary active">
            <input type="radio" name="options2" id="optionA" autocomplete="off" checked> Yes
          </label>
          <label class="btn btn-primary">
            <input type="radio" name="options2" id="optionB" autocomplete="off"> No
          </label>
        </div>
      
        <br />
        <br />
      
        <div class="btn-group-toggle" data-toggle="buttons">
          <label class="btn btn-primary">
            <input type="radio" name="options2" id="optionA" autocomplete="off"> Yes
          </label>
          <label class="btn btn-primary">
            <input type="radio" name="options2" id="optionB" autocomplete="off"> No
          </label>
        </div>
      </form>
      
      
      <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

      【讨论】:

      • 感谢混合。我遇到的问题是,如果我单击其中一个按钮,则不会提交表单。另外我不确定是否需要切换,我只需要一个普通按钮,如果单击“是”或“否”,我会立即提交
      • @TomMac 我编辑了我的代码以包含 JS,以及第三个示例以仅使用常规按钮。让我知道这是否有效!
      • 抱歉,混合 - 单击按钮时我遇到了同样的问题,即未提交任何内容。按钮看起来很棒(尤其是您包含的最后一个“普通”按钮示例)
      • @TomMac 您是否使用post 视图在后端处理表单?如果您是,我需要查看以进一步诊断
      • 哦,是的,好主意。我刚刚用views.py更新了这个问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      • 2010-10-25
      • 1970-01-01
      • 2013-04-10
      • 2015-02-04
      • 1970-01-01
      相关资源
      最近更新 更多