我正在使用js日历选择器插件将日期值传递给django视图,并基于它应该将新视图呈现到同一网页的日期。因为在这个视图中我从不同的表中提取大量数据,所以我使用ListView类。
问题是,当我在listview中使用get函数时,我能够捕获从ajax传递的数据。如何将该数据传递给get_context_data以重新生成上下文并重新加载页面?
THX
这是我的js:
<script type="text/javascript">
$(function() {
var start = moment().subtract(29, 'days');
var end = moment();
function cb(start, end) {
$('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
$.ajax({
type: 'GET',
url: "/csr/summary/",
data: {
csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val(),
from_date: from_date,
to_date: to_date,
},
success: function (response) {
}
});
console.log("done here");
}
$('#reportrange').daterangepicker({
startDate: start,
endDate: end,
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
}
}, cb);
cb(start, end);
});
</script>
以下是我的观点:
class CsrView(generic.ListView):
template_name = 'dash/csr.html'
context_object_name = 'csr_list'
from_date = ""
to_date = ""
def get(self, request, *args, **kwargs):
self.from_date = self.request.GET.get("from_date", "")
self.to_date = self.request.GET.get("to_date", "")
print "Django get method getting:", self.from_date, " - ", self.to_date
return super(CsrView, self).get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(CsrView, self).get_context_data(**kwargs)
# if self.request.method == "GET":
# print "get method triggered"
# self.from_date = self.request.GET.get("from_date", "")
# self.to_date = self.request.GET.get("to_date", "")
# context['testing'] = "lalala" + self.from_date
context['title'] = "CSR"
context['other_stuff'] = .....
答案 0 :(得分:0)
当你从get mthod调用super时,应该根据超类的超级方法调用get_context_data方法。
检查以下网址,您会在最后获取方法context = self.get_context_data()
https://ccbv.co.uk/projects/Django/1.11/django.views.generic.list/ListView/#get