我不确定我是否进行了错误配置,但在我的表单中,datepickup小部件仅在一个字段上工作(t_start)。 t_end上的小部件无法正常工作。 我不知道是什么导致了这个问题,也许有人在过去有类似的问题?
问题, 如何在表单中选择名称与数据库中的id组合,此id添加到查询中的过滤器,例如sourcetype_id__in = [3]用变量列表替换(3)。
我的表格
from django import forms
from django.forms import ModelForm, DateField, DateInput
from platforms.models import Platforms, Sources
class TimeSelection(forms.Form):
t_start = forms.DateField(widget=forms.DateInput(attrs={'id': 'datepicker'}))
t_end = forms.DateField(widget=forms.DateInput(attrs={'id': 'datepicker'}))
class PlatformSelection(forms.Form):
class Meta:
model = Platforms
platforms = forms.ModelMultipleChoiceField(queryset=Platforms.objects.all(), required=False)
class SourceSelection(forms.Form):
class Meta:
model = Sources
sources = forms.ModelMultipleChoiceField(queryset=Sources.objects.filter(pk__in=[1, 3]), required=False)
视图
def queryset(request):
t_start = None
t_end = None
platform = None
source = None
t_form = TimeSelection()
p_form = PlatformSelection()
s_form = SourceSelection()
if request.method == "POST":
t_form = TimeSelection(request.POST)
p_form = PlatformSelection(request.POST)
s_form = SourceSelection(request.POST)
if t_form.is_valid() and p_form.is_valid() and s_form.is_valid():
t_start = t_form.cleaned_data.get('t_start')
t_end = t_form.cleaned_data.get('t_end')
platform = p_form.cleaned_data.get('platforms')
source = s_form.cleaned_data.get('sources')
else:
t_form = TimeSelection()
p_form = PlatformSelection()
s_form = SourceSelection()
data = Events.objects.filter(platform_id__in=[7,8], sourcetype_id__in=[3], event_datetime__range=(t_start, t_end))
return render_to_response('platforms.html', {'t_form': t_form,
'p_form': p_form,
's_form': s_form,
'data': data,
't_start': t_start,
'platform': platform,
'source': source,
't_end': t_end,
},
context_instance=RequestContext(request))
我的表单如下:
<form method="post" action="">{% csrf_token %}
{{ p_form.as_table }}
{{ s_form.as_table }}
{{ t_form }}
<input type="submit" value="Show Charts">
</form>
答案 0 :(得分:0)
我最好的猜测是,对于日期输入窗口小部件id
,您有相同的'id': 'datepicker'
将其更改为不同的ID并且日期选择器应该有效。请记住DOM ids must be unique。