我是医生数据库并尝试实施过滤器。我想为在线预订添加一个复选框,人们可以选择过滤所有在线预订的医生。在Doctor实体中,我有一个布尔字段bookinReq
,它默认为False。在线预订的医生有真正的领域。我不确定如何在表单中实现复选框。
<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>
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))
class DropdownSelectionForm(forms.Form):
speciality = forms.ChoiceField(choices=MY_CHOICES, widget = forms.Select, required = False)
答案 0 :(得分:0)
要声明布尔值非常简单,只需添加一个线性代码即可。
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