Passing variables to a view
I've a select option list and a jstree object and I'd like to pass the user choices to a django view to process it and get the response. I'm surely doing something wrong but I don't know how to solve it. I can get the values in the scheduler view, but the template never loads, it stays in the previous view(the one who generated the template posted below).
<html>
<body>
<select id="opt" name="numbers">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input id="goButton" value="Go!" class="btn small info" />
<div id="demo2" class="demo">
<ul>
{% for car_type, cs in cars.items %}
<li>
<a>{{ car_type }}</a>
<ul>
{% for c in cs %}
<li><a id="{{c.0}}">{{c.1}}</a></li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</div>
<script>
$(function () {
$("#demo2").jstree({
.........
function submitMe(){
var checked_ids = new Array();
开发者_JAVA技巧 var car_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 car_ids = checked_ids.join(" ");
$.ajax({
type: 'POST',
url: "{% url scheduler %}",
data: {'car_ids': car_ids, 'car': car_amount},
dataType: "json"
});
};
$("#goButton").click(function() {
submitMe();
});
});
</script>
</body>
</html>
views.py(scheduler):
def scheduler(request):
if request.is_ajax():
c_ids = request.POST['car_ids'].split(" ")
cars = request.POST['car']
.......
context = {
'cars' : cars,
}
return render_to_response(
'scheduler.html',
context,
context_instance = RequestContext(request),
)
Your view returns a rendered template, but your Ajax doesn't do anything with it.
If you just want the button to take the user to a new page, you shouldn't use Ajax at all. Just put the values into a form and make the button a normal input type="submit"
.
精彩评论