0

我在这中间变得有点困惑,希望你能帮助我,

我有一个类似于下面的文本文件:

./Video/SetUp
./Video/NewRecordings
./Video/NewRecordings/20160113_151920
./Video/Back Up
./Video/Back Up/FirstLecDraft
./Video/Back Up/FirstTalk

我使用下面的 python 脚本(感谢DominateList )使用上面提到的文本文件填充一个 html 文件:

import dominate
from dominate.tags import *

doc = dominate.document(title='Dominate your HTML')

with doc.head:
    link(rel='stylesheet', href='style.css')
    script(type='text/javascript', src='script.js')

with doc:
    with div():
        with open('List') as f:
            for line in f:
                li(input(line.title(), type='submit', value='%s' % line,     onclick='self.location.href=\'http://127.0.0.1:5000/{This must be the same as "value" part}\''))

    with div():
        attr(cls='body')

print doc

第一个问题:如何将value字段的值传递给路径的其余部分onclick

结果必须是这样的:

<input href="" onclick="self.location.href='http://127.0.0.1:5000/cameradump/2016-01-21" type="submit" value="./cameradump/2016-01-21">

以及另一个按钮的另一个值。

如您所见,onclick 路径的其余部分:5000/必须与字段完全相同value

第二个问题:如何将其传递给烧瓶main.py文件中的路线?(例如当用户按下每个按钮时,该按钮的值必须动态设置为路由)

main.py现在是这样的:

from flask import Flask, render_template
import subprocess
app = Flask(__name__)

@app.route("/{value must be passed here}")
def index():
    return render_template('index.html')
 ...

但如果用户按下按钮,它应该如下所示/cameradump/2016-01-21

...
@app.route("/cameradump/2016-01-21")
def index():
return render_template('index.html')
...

或根据按下的按钮的另一个值。

4

1 回答 1

1

第一的:

做和你一样的方法value- 使用%

onclick='self.location.href="http://127.0.0.1:5000/%s"' % date

如果你不能"2016-01-21"在文件中"List"但你有"/cameradump/2016-01-21"那么你可以拆分它(使用"/")并获取最后一个元素 - 日期。

# `line` is `"/cameradump/2016-01-21"`

data = line.split('/')[-1]

第二:

阅读有关路由的文档

您可以在路线中使用变量来获取日期

@app.route("/cameradump/<date>")
def index(date):
    print("Date:", date)
    return render_template('index.html')
于 2016-01-28T22:12:56.313 回答