我有这个功能,
def event (request):
all_events = Quiz.objects.filter(owner_id=request.user.pk, status="Assigned")
get_event_types = Quiz.objects.filter(owner_id=request.user.pk, status="Assigned")
context = {"events": all_events, "get_event_types": get_event_types, }
print("context is", context)
return render(request, 'classroom/teachers/calendar.html', context)
,如果我将模板作为“ calendar.html”传递,它将运行良好。它应该绘制一个日历,并传递称为事件的上下文,然后由JavaScript函数进行处理,以使用某些数据值填充日期。
这是我正确的工作calendar.html:
{% extends 'base.html' %}
{% load fullcalendar_tags %}
{% block content %}
{% for i in events %}
{{ i }}
{% endfor %}
{% calendar %}
<script>
$(document).ready(function () {
var currentView = '',
d = new Date(),
today = d.getDay(),
dow = 1;
$('#calendar').fullCalendar({
header: {
left: 'prev,next',
center: 'title',
right: 'month,agendaWeek'
},
defaultDate: d,
events: [
{% for i in events %}
{
title: "{{ i.weight}} Kg",
start: '{{ i.scheduled_date|date:"Y-m-d" }}',
end: '{{ i.scheduled_date|date:"Y-m-d" }}',
},
{% endfor %}],
navLinks: true,
firstDay: 1, //Monday
viewRender: function (view) {
if (view && view.name !== currentView) {
if (view.name === 'agendaWeek') {
dow = today;
} else {
dow = 1;
}
setTimeout(function () {
$("#calendar").fullCalendar('option', 'firstDay', dow);
}, 10);
}
if (view) {
currentView = view.name;
}
}
});
});
</script>
{% endblock %}
这使上下文事件正常。但是,当我尝试将其传递给另一个视图时,{%calendar%}部分工作正常,但是根本没有发送上下文。我已经打印了很多次,无论出于什么原因,它都没有在“事件”上下文中传递任何内容。
我从另一种观点找到了一种调用事件函数的方法(不太确定是否有最好的方法,但是执行起来似乎很简单)。
def json_example1 (request):
distinct_origin = Quiz.objects.filter(owner_id=request.user.pk).order_by().values_list('origin',
flat=True).distinct()
distinct_o_list = list(distinct_origin)
distinct_dest = Quiz.objects.filter(owner_id=request.user.pk).order_by().values_list('destination',
flat=True).distinct()
distinct_d_list = list(distinct_dest)
markers_list = list(set(distinct_o_list) | set(distinct_d_list))
# print("markers", markers_list)
gmaps = googlemaps.Client(key='AIzaSyArNLCXScd7tH0mwlDDPqXoVNgTT6emblI')
latitude = list()
longitude = list()
for i in markers_list:
geocode_result = gmaps.geocode(i)
lat = geocode_result[0]["geometry"]["location"]["lat"]
lon = geocode_result[0]["geometry"]["location"]["lng"]
longitude.append(lat)
latitude.append(lon)
# print("lat", latitude)
data = pd.DataFrame({'lat': latitude, 'lon': longitude, 'name': markers_list})
m = folium.Map(location=[21, 78], tiles="OpenStreetMap", zoom_start=4.75)
for i in range(0, len(data)):
folium.Marker([data.iloc[i]['lon'], data.iloc[i]['lat']], popup=data.iloc[i]['name']).add_to(m)
html_string = m._repr_html_()
context = {'map2': html_string}
# print("context is", context)
def load_calendar (request):
return event(request)
load_calendar(request) //Here I am loading the function
return render(request, 'classroom/teachers/graph_tot_trucks.html', context)
任何线索??
答案 0 :(得分:0)
我能够使用iframe完美地做到这一点。所有上下文数据也都可以通过。很奇怪,我从未在SO或Google上找到关于此的任何线索。