【问题标题】:How to replace old value with new value in template using python?如何使用python在模板中用新值替换旧值?
【发布时间】: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


【解决方案1】:

您可以在其official tutorial 中查看如何将变量添加到 django 模板上下文中。您可以使用Django Debug Toolbar 轻松调试此上下文。

您可能想使用login_required decorator 并依靠 django 身份验证系统来设置登录模板。另一个建议:不要使用print 进行调试,您可以使用logging modulesnippet 进行快速查找。

注意:您的问题有一个令人困惑的代码,我认为您将某些部分粘贴了两次

【讨论】:

  • 我的代码不是两次。我需要的是如何将 python 变量值发送到 html 文件?
  • 缩进错了?你检查了我给你的所有文档吗?已经测试过 Django 调试工具栏?
  • 你没有回答我的问题。这是讨论这个问题的正确地方。以后更多人可以看到作为参考
  • 实际我在window机上开发如何在windows中安装调试工具栏?
  • 如果你看到了,我想我的问题很简单。
【解决方案2】:

试试这个:

from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponseForbidden
import json
@csrf_exempt
def default(request):
    if request.is_ajax() and request.user.is_authenticated() and 'phone_no' in request.POST:
        phone_no = request.POST['phone_no']
        dummy_var = 'test'
        dict = {'phone_no': phone_no}

        context = {'phone_no': phone_no, 'dict': dict, 'dummy_var ': dummy_var}
        return render(request, 'mobile_recharge.html', context)
    else:
        HttpResponseForbidden()

如果您确实需要打印变量,请改用日志记录。

【讨论】:

  • 我仍然没有在 html 文件中得到任何值。当用户输入 10 位手机号码时,ajax 函数会调用 python 函数已经不是第一次了。 python 函数不会将结果发送到 html 文件。当我打印值时,它会在日志文件中显示值
猜你喜欢
  • 2021-07-11
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 2012-01-19
  • 2015-08-01
  • 2020-08-03
  • 1970-01-01
相关资源
最近更新 更多