【问题标题】:How can I display my python file in html?如何在 html 中显示我的 python 文件?
【发布时间】:2016-10-11 15:36:47
【问题描述】:

我正在使用烧瓶,尝试了不同的示例,许多代码但没有任何效果..

这是我的html:

<form method="post" name="prueba">

                        <div class="form-group ">
                            <label class="col-sm-3 col-sm-3 control-label">Direccion IP: </label>
                            <div class="col-sm-9">
                                 <input type="text" class="form-control" value="{{ address }}" >
                             </div>
                         </div>

python 文件:

def get_info():

    with open('/etc/network/interfaces', 'r+') as f:
        for line in f:
            found_address = line.find('address')
            if found_address != -1:
                address = line[found_address+len('address:'):]
                print 'Address: ', address
            found_network = line.find('network')
            if found_network != -1:
               network = line[found_network+len('network:'):]
               print 'Network: ', network
            found_netmask = line.find('netmask')
            if found_netmask != -1:
               netmask = line[found_netmask+len('netmask:'):]
               print 'Netmask: ', netmask 
            found_broadcast = line.find('broadcast')
            if found_broadcast != -1:
               broadcast = line[found_broadcast+len('broadcast:'):]
               print 'Broadcast: ', broadcast
    return address 
print get_info()

@app.route('/test')
def showPage():
    addresses = get_info()
    return render_template('test.html', addresses=addresses)

当我通过控制台运行 python 文件时它可以正常工作,但是当我尝试在我的表单中显示它时没有任何反应。

【问题讨论】:

  • 如果多于1个地址,尝试使用{% for %}
  • 我尝试使用 {% for %} 但没有。
  • 您是否正确导入了 render_template?尝试在 /test 路由中浏览时是否有任何错误?
  • 不,它在 /test 路由中运行良好。我不知道为什么我不能在表单中显示我的地址。也许它没有从我的项目中正确打开 /etc/network/interfaces?
  • 尝试在 {% if addresses %} {% endif %} 中放入一些 html 以查看模板引擎是否正在识别此变量。请记住使用您传递的相同名称,在这种情况下是地址,而不是地址

标签: python html python-2.7 flask debian


【解决方案1】:

是否您将数据作为addresses 提供给模板,但您在模板中调用了变量address?

【讨论】:

    猜你喜欢
    • 2019-05-30
    • 2019-06-30
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多