我有两个过滤器(选择1和选择2)。我可以同时选择一个或两个。一个过滤器位于日期年份(datumObjave),另一个过滤器位于名称(naslov)。我的问题是这些过滤器当前处于固定值。
我不知道如何启用复选框,它会向我显示文本框,在其中输入将用作过滤器参数的文本。
footer.html:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body>
<form action="" method="POST">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Apply" />
</form>
<script>
$(document).ready(function(){
var counter = 0;
$('#choice_0').click(function() {
if (!counter) {
$('<input>').attr({
id: 'foo',
name: 'text'
}).appendTo('form');
}
counter++;
})
var counter = 0;
$('#choice_1').click(function() {
if (!counter) {
$('<input>').attr({
id: 'foo',
name: 'text'
}).appendTo('form');
}
counter++;
})
});
</script>
</body>
</html>
forms.py:
class ChoiceForm(forms.Form):
filter = forms.MultipleChoiceField(choices=(('1', 'Choice 1'), ('2', 'Choice 2')), widget=forms.CheckboxSelectMultiple(attrs={'id': 'choice'}))
name = forms.TextInput()
year = forms.TextInput()
views.py:
def filtar(request):
form = ChoiceForm(request.GET or None)
data = Clanak.objects.all()
print(request.POST) # print QueryDict also
if form.is_valid():
print(form.cleaned_data) # This dict contains keys (filters, and html(This is dynamically added field))
if '1' in form.cleaned_data['filter']:
data = data.filter(naslov=form.cleaned_data['name'])
if '2' in form.cleaned_data['filter']:
data = data.filter(datumObjave__year=form.cleaned_data['year'])
return render(request, 'filtar.html', {'data': data, 'form': form})
urls.py:
urlpatterns = [
path('filtar/',views.filtar, name='filtar'),
] + static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)
屏幕截图:
-----------------------更新----------------------- -------:
我更新了代码,现在有两个问题:
1。)无论我启用还是禁用复选框,它总是会添加2个新的文本字段
2。)它不过滤任何内容,它始终显示所有数据
答案 0 :(得分:1)
我将向您展示一个小例子,
from django import forms
class MyForm(forms.Form):
filters = forms.MultipleChoiceField(
choices=(('choice1', 'Choice 1'), ('choice2', 'Choice 2')),
widget=forms.CheckboxSelectMultiple(attrs={'id': 'choice'})
)
html = forms.TextInput()
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body>
<form action="" method="POST">
<!-- no {#% csrf_token %#} required if you send HTTP GET request -->
{{ form.as_p }}
<input type="submit" value="click" />
</form>
<script>
<!-- when you click now on checkbox, input will be generated inside your form. name: 'html' because I have html field(TextField) in my forms. This is for mapping -->
$(document).ready(function(){
var counter = 0;
$('#choice').click(function() {
if (!counter) {
$('<input>').attr({
id: 'foo',
name: 'text'
}).appendTo('form');
}
counter++;
})
});
</script>
</body>
</html>
def filtar(request):
form = ChoiceForm(data=request.GET or None)
data = Clanak.objects.all()
print(request.POST) # print QueryDict also
if form.is_valid():
print(form.cleaned_data) # This dict contains keys (filters, and html(This is dynamically added field))
if 'choice1' in form.cleaned_data['filters']:
data = data.filter(naslov=form.cleaned_data['html'])
if 'choice2' in form.cleaned_data['filters']:
data = data.filter(datumObjave__year=2019)
return render(request, 'filtar.html', {'data': data, 'form': form})
分析代码。