Python DJANGO将新结果值从AJAX请求呈现到HTML页面
在过去的几天里,我试图学习在后端使用AJAX和django提交表单 我可以在django views.py(Python DJANGO将新结果值从AJAX请求呈现到HTML页面,python,jquery,html,ajax,django,Python,Jquery,Html,Ajax,Django,在过去的几天里,我试图学习在后端使用AJAX和django提交表单 我可以在django views.py(validate\u number)中使用AJAX成功获取表单输入值(工作)如下所示。 在这个views.pyvalidate_number中,我计算了新的数字总和,我希望这个总和值返回到html页面,但我不知道怎么做 知道如何将AJAX请求的结果呈现回html页面吗 这是密码 html格式: <form id="form" action='' data-validate-numbe
validate\u number
)中使用AJAX成功获取表单输入值(工作)如下所示。
在这个views.pyvalidate_number
中,我计算了新的数字总和,我希望这个总和值返回到html页面,但我不知道怎么做
知道如何将AJAX请求的结果呈现回html页面吗
这是密码
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>{{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>
URL.py:
url(r'^details/(?P<slug>[^\.]+)/$', views.blog_details, name='blog_details'),
url(r'^ajax/validate_number/$', views.validate_number, name='validate_number'),
在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>
这是我们渲染到\u字符串以发送到AJAX的模板。它以与渲染
相同的方式渲染模板
sum_template.html
{{ sum }}
您不希望将_呈现为_string
index.html
,因为您将整个索引
模板插入
,而不仅仅是总和
。您可能还想在视图中添加if
语句
if request.is_ajax() and request.POST:
过滤掉非AJAX请求
有人告诉我有更好的方法。这一切我都是自己想出来的,不知道是什么。如果您需要更多详细信息,请告诉我。在您的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>
这是我们渲染到\u字符串以发送到AJAX的模板。它以与渲染
相同的方式渲染模板
sum_template.html
{{ sum }}
您不希望将_呈现为_string
index.html
,因为您将整个索引
模板插入
,而不仅仅是总和
。您可能还想在视图中添加if
语句
if request.is_ajax() and request.POST:
过滤掉非AJAX请求
有人告诉我有更好的方法。这一切我都是自己想出来的,不知道是什么。如果您需要更详细的信息,请告诉我。我将在这里介绍一个最小的计算应用程序。我们给出两个带有wish操作的数字,django进行计算并返回json格式的答案。
注意,我使用了ajax/jquery回调概念,并使用csrf_在django视图中禁用了csrf控件
HTML/javaScript:
<div class="container">
<form class="col-lg-6" id="form">
<legend>Select number to make an operation</legend>
Number 1: <input type="number" class="form-control" id="n1">
Number 2: <input type="number" class="form-control" id="n2">
Select operation:
<select class="form-control" name="op" id="op">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
</select>
<input type="submit" id="submit" value="Send">
</form>
<div>
<h2 class="result-box">The result is : <strong id="result"></strong></h2>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(function() {
function call_ajax(f) {
const n1 = $('#n1').val();
const n2 = $('#n2').val();
const op = $('#form option:selected').text();
const data = {n1: n1, n2: n2, op: op}
// you can verify the data in the browser console
console.log(data);
$.ajax({
url: '/ajax/make_operation/',
data: data,
type: 'POST',
success: f,
error: function(error) {
console.log(error);
}
});
}
function server_response(response) {
// convert to json format
const r = JSON.parse(response);
console.log(r);
// include the result in the dom
var text = document.createElement('i');
text.innerHTML = '<strong id="result">' + r.result + '</strong>';
$('#result').replaceWith(text);
}
//Validate
$('#form').submit(function(e) {
e.preventDefault();
call_ajax(server_response);
});
});
</script>
</body>
</html>
url.py
from django.contrib import admin
from django.urls import path
from ajax import views
urlpatterns = [
path('admin/', admin.site.urls),
path('ajax/make_operation/', views.make_operation),
]
因此,有很多选择可以做到这一点。我只展示了一种使用django实现ajax的方法(没有django表单)。我将在这里介绍一个最小的计算应用程序。我们给出两个带有wish操作的数字,django进行计算并返回json格式的答案。
注意,我使用了ajax/jquery回调概念,并使用csrf_在django视图中禁用了csrf控件
HTML/javaScript:
<div class="container">
<form class="col-lg-6" id="form">
<legend>Select number to make an operation</legend>
Number 1: <input type="number" class="form-control" id="n1">
Number 2: <input type="number" class="form-control" id="n2">
Select operation:
<select class="form-control" name="op" id="op">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
</select>
<input type="submit" id="submit" value="Send">
</form>
<div>
<h2 class="result-box">The result is : <strong id="result"></strong></h2>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(function() {
function call_ajax(f) {
const n1 = $('#n1').val();
const n2 = $('#n2').val();
const op = $('#form option:selected').text();
const data = {n1: n1, n2: n2, op: op}
// you can verify the data in the browser console
console.log(data);
$.ajax({
url: '/ajax/make_operation/',
data: data,
type: 'POST',
success: f,
error: function(error) {
console.log(error);
}
});
}
function server_response(response) {
// convert to json format
const r = JSON.parse(response);
console.log(r);
// include the result in the dom
var text = document.createElement('i');
text.innerHTML = '<strong id="result">' + r.result + '</strong>';
$('#result').replaceWith(text);
}
//Validate
$('#form').submit(function(e) {
e.preventDefault();
call_ajax(server_response);
});
});
</script>
</body>
</html>
url.py
from django.contrib import admin
from django.urls import path
from ajax import views
urlpatterns = [
path('admin/', admin.site.urls),
path('ajax/make_operation/', views.make_operation),
]
因此,有很多选择可以做到这一点。我只展示了一种使用django实现ajax的方法(没有django表单)。如何将sum从views.py发送到ajax成功块?抱歉。更新的答案我看到你还没有标出答案。我又更新了我的。我知道这项工作,你能解释一下这项工作是如何处理多个值的吗?例如,如果我有sum1、sum2、sum3?我试着使用这样的东西:sum_json_1=render_to_string('index.html'、{'sum1':sum1})sum_json_2=render_to_string('index.html'、{'sum2':sum2})返回JsonResponse({'sum_json_json_1':sum_json_1'、'sum_json__2':sum___2})
和在ajax中:$('#sum1).html(data.sum_json_1)$html(data.sum#u json_2)代码>但不是工作如何将sum从views.py发送到AJAX成功块?抱歉。更新的答案我看到你还没有标出答案。我又更新了我的。我知道这项工作,你能解释一下这项工作是如何处理多个值的吗?例如,如果我有sum1、sum2、sum3?我试着使用这样的东西:sum_json_1=render_to_string('index.html'、{'sum1':sum1})sum_json_2=render_to_string('index.html'、{'sum2':sum2})返回JsonResponse({'sum_json_json_1':sum_json_1'、'sum_json__2':sum___2})
和在ajax中:$('#sum1).html(data.sum_json_1)$html(data.sum#u json_2)代码>但不是工作你给我看了一个很好的例子谢谢,但在我的例子中我有两个不同的视图。py,怎么做?你给我看了一个很好的例子谢谢,但在我的例子中我有两个不同的视图。py,怎么做?