首页 > 解决方案 > 变量没有出现在模板python上

问题描述

1)。我有一个用户电子邮件的下拉列表。当我选择其中一个时,此电子邮件会出现模式窗口,我可以将消息发送到此电子邮件。但是,当我发送它时,我得到了错误:'NoneType' object is not subscriptable。问题是什么?我的对象不是空的。(我得到它ajax)我可以在控制台中获取我的电子邮件。

2)。第二个问题是我无法在页面上获取born_datephone_number,但我可以在控制台中打印它们。有问题render_template。请帮忙。

数据库结构:

在此处输入图像描述

@app.route('/profile', methods=['GET','POST'])
def profile():
  if request.method == 'POST':
     print('Holy Shit!')
     data = request.json
     print(str_value_to_list(data['selectedItems'][0]))

     cur = mysql.connection.cursor()
     cur.execute("SELECT born_date, phone_number FROM users.data WHERE email = '%s'" % 
     str_value_to_list(data['selectedItems'][0]))
     account = cur.fetchone()
     born = account[0]
     num = account[1]
     print(born)
     print(num)
     return render_template('profile.html', born=born, num=num)

html:

<form action="profile" method="POST" enctype="multipart/form-data">
<div class="container reg" style="width: 45%;">
  <div class="form-wrapper">
    <p class="white-text" style="color: #000;">Search your email</p>
     <br>


    <button type="button" class="btn btn-primary" onclick="process();" style="background:#3E75A5; border: 1px solid #285F8F;">
        Get Info
    </button>
   
<script type="text/javascript">
    function printValue(selectedItem){
       $('#mySelectedValue').html(selectedItem.value);
    }
</script>    
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">Information about user</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p class="form-control" id="mySelectedValue" style="margin-top: 10px;"></p>

        <input type="text" name="text" class="form-control" placeholder="Add your message here" required="required">

        <h2 class="white-text" style="font-size: 14px; color: #000;">born: {{ born }}</h2>
        <h2 class="white-text" style="font-size: 14px; color: #000;">num: {{ num }}</h2>

        <button class="btn btn-primary send">
            Send
        </button>

      </div>

    </div>
  </div>
</div> 

<script type="text/javascript">
function printValue(selectedItem) {
    $('#mySelectedValue').html(selectedItem.value.replace(/[{()}]/g, '').replace(/['"]+/g, '').replace(/[{,}]/g, ''));
    console.log(selectedItem.value);
}
function process(selectedItem) {
    $('#exampleModalCenter').modal('show')
    document.getElementById('#exampleModalCenter')
    const data = JSON.stringify({
        "selectedItems": $('#sel').val()
    });
    $.ajax({
        url: "/profile",
        type: "POST",
        contentType: "application/json",
        data: data,
        success: function (data) {
          console.log(data);
        },
    });
}
function optionClick(selectedItem) {
    printValue(selectedItem);
}
</script>
  </div>
</div>
</form>

标签: pythonhtmlpython-3.xflask

解决方案


推荐阅读