def graph(request): print(request.GET.get('country', '')) graphData = request.GET.get('data', '') request.session['country'] = request.GET.get('country', '') request.session['graph'] = Graph(graphData).get_context_data( request.GET.get('country', ''))['graph'] return HttpResponse("ok")
def YFV(request): graph = Graph('YFV') context = graph.get_context_data(country='Angola') try: if request.session['data'] != "YFV": request.session['country'] = "Angola" request.session['graph'] = context['graph'] context['graph'] = "<div id='graph'>" + request.session[ 'graph'] + "</div>" else: context['graph'] = "<div id='graph'>" + request.session[ 'graph'] + "</div>" except KeyError: request.session['country'] = "Angola" request.session['graph'] = context['graph'] context[ 'graph'] = "<div id='graph'>" + request.session['graph'] + "</div>" request.session['data'] = "YFV" context["scripts"] = """<script> console.log("Test") var val = '""" + request.session['country'] + """'; console.log(val) var sel = document.getElementById('country'); var opts = sel.getElementsByTagName('option'); for (var opt, j = 0; opt = opts[j]; j++) { if (opt.text == val) { sel.selectedIndex = j; break; } } function GetCountry() { var xhr = new XMLHttpRequest(); var dd = document.getElementById('country'); var data = JSON.stringify({'country': dd.getElementsByTagName('option')[dd.selectedIndex].text}) console.log(data); var url = window.location.href.replace("/YFV","")+ "/graph?country=" + dd.getElementsByTagName('option')[dd.selectedIndex].text+"&data=YFV" console.log(window.location.href.replace("/YFV","")) xhr.open("GET", url, true); xhr.setRequestHeader("Content-type","application/json"); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { // do something with response location.reload(); console.log(xhr.responseText); } }; xhr.send(); } </script>""" context["countryList"] = graph.getCountryList() print(context) return TemplateResponse(request, "graph.html", context)