嗨,朋友们,我需要帮助,因为我是django的新手!我需要根据从select
中选择的选项显示我保存在表中的信息。当选择模板中的选择选项时,我使用JavaScript将值保存在变量中(我这样做没有问题)。我不能将该变量的值获取到get_context_data
方法中,以便稍后在我的模板中显示它。JavaScript代码:
$(function () {
$('select[name="reporteFecha"]').on('change', function () {
var fecha = $(this).val();
$.ajax({
url: window.location.pathname,
type: 'POST',
data: {
'action': 'search_fecha',
'fecha': fecha
},
dataType: 'json',
})
});
});
视图代码:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
### I need to obtain within this function the value of the select in a variable ###
CE = ConsumoElect.objects.filter(fecha = *value obtained from JavaScript*)
context = {
'CE': CE,
'title': 'Reporte de Control de Electricidad',
'entity': 'Reporte Electricidad',
'list_url': reverse_lazy('cont_elect:Reporte_list'),
'form': ReportForm(),
}
return context
我需要在get_context_data
中的一个变量中获取我用JavaScript捕获的值,以便以后能够在模板中显示它
2条答案
按热度按时间xdnvmnnf1#
get_context_data用于GET请求,而不是POST请求。它基本上不是用于计算或管理POST的呈现。要做到这一点,您必须重新定义post()方法。在您的情况下,您将有类似的内容:
aiqt4smr2#
看了一些视频教程,我发现一个帮助我解决问题的方法(虽然它没有使用JavaScript),该方法不是视图的类而是函数。