簡體   English   中英

如何從python文件獲取值到html?

[英]How can I get values from python file to my html?

我正在嘗試將值從python文件獲取到html。

這是我的表格:

<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 getinterfaces():
    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
            return address

我嘗試使用“ FLASK”,但沒有任何效果。

我認為我的問題出在我的道路上,因為我的項目在“ project / app / Mensajeria / views.py”中,我的html在:“ project / app / templates / test.html”

我正在導入Flask以及所有內容,已經安裝了燒瓶,我試圖重新安裝,但沒有任何效果...

如果您要使用HTML模板發送要在網頁上提供的數據,那么類似的情況又如何呢?

@app.route('/test')
def showPage():
    # your code here
    address = # however you assign it
    return render_template('test.html', address=address)

為了在模板的{{}}中使用變量,您必須將該變量傳遞給模板,例如在這里使用return render_template('test.html', address=address)

如果您需要address包含許多內容,則可以始終將其作為listdict發送,並使用jinja提取模板中所需的各個變量。 例如,如果您需要多個<div>元素,每個元素都包含一個地址:

python文件

@app.route('/test')
def showPage():
    # your code here
    addresses = # however you assign it
    return render_template('test.html', addresses=addresses)

test.html

{% for address in addresses %}
    <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>
{% endfor %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM