В вашем блоке успеха AJAX вы должны сказать ему, что вы хотите, чтобы он делал с информацией:
$(".next-step1").click(function (e) {
var form = $(this).closest("form");
var number1 = $("#CC").val();
var number2 = $("#PP").val();
$.ajax({
url: form.attr("data-validate-number-url"),
data: {
'number1': number1,
'number2':number2
},
dataType: 'json',
success: function (data) {
// 'data' is the dictionary received from the view.
// You could call it whatever you want.
$('#sum).html(data.sum_json);
/* Find 'id="sum"' and replace what's inside the tags(or innerHTML)
with the dictionary value of 'sum_json'.
If other keys exist in the 'data' dictionary,
they are accessed the same way, as in 'data.sum_json_2'.
*/
}
});
index.html
<form id="form" action='' data-validate-number-url="{% url 'validate_number' %}" method="POST" enctype="multipart/form-data">{% csrf_token %}
<div class="form-group col-md-6">
Select the C Raster Dataset:<br>
<select name="CC" class="form-control" id="CC">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
Select the P:<br>
<select name="PP" class="form-control" id="PP">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
Select the F:<br>
<select name="FF" class="form-control" id="FF">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
</div>
<button type="button" class="btn btn-primary next-step1">Save and continue</button>
<p id="sum"></p>
Select the GG:<br>
<select name="G" class="form-control" id="GG">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
Select the JJ:<br>
<select name="JJ" class="form-control" id="JJ">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
<button type="button" class="btn btn-primary next-step">Save and continue</button>
Select the FINAL:<br>
<select name="FINAL" class="form-control" id="FINAL">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="10">ten</option>
</select>
<button type="submit" class="btn btn-primary">Save</button>
</form>
вид
def blog_details(request,slug):
posts=mymodel.objects.all()
post=get_object_or_404(posts, slug_title=slug)
return render(request,'index.html',{'post':post})
def validate_number(request):
# You had request.GET, but your form method is POST.
number1 = request.POST.get('number1', None)
print number1
number2 = request.POST.get('number2', None)
print number2
sum=int(number1)+int(number2)
# render a template with the given key: value to be sent to AJAX.
sum_json = render_to_string('sum_template.html', {'sum': sum})
"""
Assuming the information for sum_2 etc. uses the same format,
we can use the same template
"""
sum_json_2 = render_to_string('sum_template.html', {'sum': sum_2})
# Send the dictionary to AJAX. This is what we called 'data'.
return JsonResponse({'sum_json': sum_json, 'sum_json_2': sum_json_2})
Это шаблон, который мы render_to_string
отправляем в AJAX. Шаблоны отображаются так же, как render
.
sum_template.html
{{ sum }}
Вы не хотите render_to_string
index.html
, потому что вы вставляете весь шаблон index
в <p>
, а не просто sum
. Вы, вероятно, также хотите добавить к своему представлению оператор if
if request.is_ajax() and request.POST:
для фильтрации не AJAX-запросов.
Мне сказали, что есть лучшие способы сделать это. Я сам все понял и не знаю, кто они. Если вам нужно больше деталей, дайте мне знать.