I'm using jquery .submit() function to calculate two variables(f_amount and f_variables) 开发者_StackOverflow社区but I don't know how to pass them to the next django view.
<form method="POST" action="/app/view">{% csrf_token %}
<select id="fd" name="fd">
<option value="2">2</option>
<option value="3">3</option>
<option value="5">5</option>
<option value="1">1</option>
</select>
<div id="demo2" class="demo">
<ul>
{% for f_type, fs in f.items %}
<li>
<a>{{ f_type }}</a>
<ul>
{% for f in fs %}
<li><a id="{{f.0}}">{{f.1}}</a></li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</div>
<input id="plan" type="submit" value="sched it!" class="btn primary" />
</form>
<script>
$("form").submit(function() {
var checked_ids = new Array();
var f_amount = $("select option:selected").val();
$("#demo2").jstree("get_checked",null,true).each
(function (index,value) {
if (value.children[2].id) {
checked_ids.push(value.children[2].id);
}
});
var f_ids = checked_ids.join(" ");
});
</script>
i could do it this way in Django 2.1
x123.html First create a hidden input field like this in the 1st html file
<form id="scoresForm" method="POST">
{% csrf_token %}
<input type="hidden" name="scores" value=""/>
</form>
views.py then define your View in the views.py and do the POST data handling
def result(request, league_id, season_id, matchday):
scores = request.POST.get("scores", None)
if scores != None:
scores = scores.replace(""", "'")
#scores = scores[1:-1]
scores = json.loads(scores)
context = {
'league_id': league_id,
'season_id': season_id,
'matchday': matchday,
'scores': scores,
}
return render(request, 'matchstatistics/result.html', context)
x123.js and lastly in your JS file which belongs to your 1st html template
var resultUrl = currentUrl + "/result/" + league_id + "/" + season_id + "/" + matchday + "/";
var scoresForm = document.forms['scoresForm'];
scoresForm.action = resultUrl;
scoresForm.elements["scores"].value = JSON.stringify(scores);
document.getElementById("scoresForm").submit();
y123.html You are able to access the passed Array/Variable easily in your following view by calling f.e. {{ scores }} in your Django Template
Please feel free to optimize the code, especially the JSON-Object serialization and deserialization part, im new to Django/Python/JavaScript :)
精彩评论