我使用object_list表创建一个模板 我需要按子字符串过滤,然后当用户过滤表格时将在线更新。
def filters_points(请求):
point_list = Point.objects.all()
try:
req = request.GET.get("filterData")
if req:
point_list = Point.objects.filter(onsitePointName__icontains=req)
except AttributeError:
print("failed")
#qs_json = serializers.serialize('json', point_list)
#return HttpResponse(qs_json, content_type='application/json')
return render_to_response('point/point_list_update.html', {'point_list': point_list})
答案 0 :(得分:0)
选项1: 您可以使用django docs
中的自定义模板过滤器和标签 from django import template
register = template.Library()
@register.assignment_tag
def get_filtered_points(qs, **kwargs):
return qs.filter(**kwargs)
模板:
{% get_filtered_points your_filter_query as new_point_list %}
{% for book in new_point_list %}
...
{% endfor %}
注意:不要忘记在模板中加载模板标签。
选项2:您也可以使用intercularjs
解释here
答案 1 :(得分:0)
以django形式构建过滤器的输入类型: filterBySubstring = forms.CharField(label ='substring',max_length = 100,required = True)
然后通过以下方式在python代码中获取此值: form = PointForm(request.POST)
if request.method == 'POST':
filter_value = form['filterBySubstring'].value()