【发布时间】:2017-07-07 22:50:22
【问题描述】:
我需要在模板文件中将旧值更改为新值。
实际上,当用户输入 10 位手机号码时,python 函数会调用。如果我签入日志它显示值,但在 html 文件中它没有显示值。如何在views.py中第二次调用函数获取该值?
views.py
from django.views.decorators.csrf import csrf_exempt
import json
@csrf_exempt
def default(request):
if request.is_ajax():
print("inside the ajax")
phone_no = request.POST.get('phone_no')
print(phone_no)
dict = {'phone_no': phone_no}
template_name = 'mobile_recharge.html'
extended_template = 'base.html'
dummy_var = 'test'
print(dummy_var)
if request.user.is_authenticated():
extended_template = 'base_login.html'
return render(request,template_name, {'extended_template': extended_template,'phone_no': phone_no,'dummy_var': dummy_var})
template_name = 'mobile_recharge.html'
extended_template = 'base.html'
dummy_var = 'test'
if request.user.is_authenticated():
extended_template = 'base_login.html'
return render(
request, template_name,
{'extended_template': extended_template,'dummy_var': dummy_var}
)
脚本
<script type="text/javascript">
$(document).ready(function () {
$('#phone_number').on('input', function () {
if ($(this).val().length >= 10) {
alert("user enter 10 digits numbers");
var phone_no = $('#phone_number').val()
console.log(phone_no);
$.ajax({
type: "POST",
url: "http://localhost:8090/",
data: {'phone_no' : phone_no},
dataType: "json",
success: function(msg)
{
//alert(msg.phone_no);
}
});
return;
}
});
});
</script>
html
{{phone_no}} {{ dict }} {{dummy_var}}
如果我在模板中调用上述变量。我没有得到任何结果。如何得到结果?
【问题讨论】:
-
能否请您再次阅读您的问题并更正一下?
-
根据我对您正在尝试做的事情的了解,我想说,要么重新加载页面,要么使用您的 javascript 更新 phone_no。目前无处更新。
-
我想将 dummy_var 结果传递给 html 文件。该怎么做?
标签: jquery python html ajax django