【问题标题】:Django checkbox in forms to filter objects表单中的 Django 复选框以过滤对象
【发布时间】:2015-09-09 22:47:14
【问题描述】:

我有一个医生数据库并尝试实现过滤器。我想为在线预订添加一个复选框,人们可以选择它来过滤所有可在线预订的医生。在 Doctor 实体中,我有一个布尔字段 bookinReq,默认为 False。在线预约的医生拥有 True 的字段。我不确定如何在表单中实现复选框。

doclisting.html

        <select class="form-control" id="speciality" name="speciality">
          <option value="All Doctors"><b>Choose Speciality...</b></option>
          {% for value, text in form.speciality.field.choices %}
            {% if value == s_name %}
              <option selected="selected" value="{{ value }}">{{ text }}</option>
            {% else %}
              <option value="{{ value }}">{{ text }}</option>
            {% endif %}
          {% endfor %}
        </select>



        <select class="form-control" id="language" name="language">
            <option><b>Choose Language</b></option>
            {% for l in languages %}
              {% if l.name == l_name %}
                <option selected="selected" value="{{ l }}">{{ l }}</option>
              {% else %}
                <option value="{{ l }}">{{ l }}</option>
              {% endif %}
            {% endfor %}
        </select>
         <div class="form-control" id="bookinReq" name="bookinReq">
            <label>
               <input type="checkbox" name="bookingReq">  Online Booking
            </label>
          </div>

        <span class="input-group-btn">
          <button class="btn btn-primary" type="submit" id="ss-submit">Search</button>
        </span>
      </form>          

views.py

def filter_doctors(request=None, specialization=None, language=None bookinReq=None):

    query = Doctor.objects.filter()

    if specialization and specialization != "All Doctors":
        try:
            spec = Specialization.objects.get(name = specialization) 
            query = query.filter(specialization=spec)
        except:
            return None
    if language and language != "Choose Language":
        try:
            lang = Language.objects.get(name=language)
            query = query.filter(language=lang)
        except:
            return None
    if bookinReq is True:
        query = query.filter(bookinReq = True)
    return query



def doclistings(request): 
    d = getVariables(request)
    if request.method == "GET":
        form = DropdownSelectionForm(request.GET)
        try:
            s_name = request.GET['speciality']
        except:
            s_name = None
        try:
            l_name = request.GET['language']
        except:
            l_name = None

        try:
            bookinReq = request.GET['bookinReq']
        except:
            bookinReq = None

        d['s_name'] = s_name 
        d['l_name'] = l_name
        d['bookinReq'] = bookinReq

        try:
            doctors = filter_doctors(request=request, specialization=s_name, language=l_name, bookinReq = bookinReq)

        except Exception:
            return error404(request)

        if doctors == None: # error during filteration, rended 404
            return error404(request)

        if len(doctors) == 0:
            d['not_found'] = "anything you want here :)"


    else:
        form = DropdownSelectionForm()


    d.update({'form': form, 'languages': Language.objects.all()})
    return render_to_response('m1/doclistings.html',d, context_instance=RequestContext(request))

forms.py

class DropdownSelectionForm(forms.Form):
    speciality = forms.ChoiceField(choices=MY_CHOICES, widget = forms.Select, required = False)

【问题讨论】:

  • 能否显示 DropdownSelectionForm 代码?
  • 我已经用表单更新了代码

标签: python django forms checkbox


【解决方案1】:

要声明一个布尔值非常简单,只需添加这一行代码。

bookinReq = forms.BooleanField(required=True, initial=False)

除了您的问题之外,还有一些事情可能会在将来的某个时间为您节省时间。

try:
    s_name = request.GET['speciality']
except:
    s_name = None

# That block of code can be replace with
s_name = request.GET.get('speciality', None)    # the second param is the default value

呈现表单的方式需要大量代码。您应该考虑从 dj 网站查看表单渲染。更好的方法是脆的形式,一旦你使用它就不想使用其他任何东西了。

https://docs.djangoproject.com/en/1.8/topics/forms/ http://django-crispy-forms.readthedocs.org/en/latest/

<select class="form-control" id="speciality" name="speciality">
  <option value="All Doctors"><b>Choose Speciality...</b></option>
  {% for value, text in form.speciality.field.choices %}
    {% if value == s_name %}
      <option selected="selected" value="{{ value }}">{{ text }}</option>
    {% else %}
      <option value="{{ value }}">{{ text }}</option>
    {% endif %}
  {% endfor %}
</select>

# here is some what equivalent version
<select class="form-control" id="speciality" name="speciality">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>

这都是我的,祝你好运:D

【讨论】:

  • 感谢您的回答。有没有办法实现没有脆表单的复选框?
  • 那个语法不脆,是django原生的
猜你喜欢
  • 2014-02-10
  • 2021-11-27
  • 1970-01-01
  • 2011-11-04
  • 2021-07-18
  • 2021-08-18
  • 2017-06-29
  • 2018-05-31
  • 2020-03-06
相关资源
最近更新 更多