使用用户指定的路径

2024-09-27 19:10:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个窗体,并希望允许用户指定一个路径

我试图创建一个表单,其中包含一个字段,允许用户指定一个路径名,然后由python函数继续使用。目前我只是在HTML模板中使用一个文本表单字段:

<br>
<label for="userpath">Path name for files:</label>
<input type="text" class="form-control" id="userpath" name="userpath" placeholder="Provide path name">
<br>

然后在我的python脚本中,我根据这个用户输入执行一些代码

from random import randint
from time import strftime
from flask import Flask, render_template, flash, request
from wtforms import Form, TextField, TextAreaField, validators, StringField, SubmitField

DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
app.config['SECRET_KEY'] = 'SjdnUends821Jsdlkvxh391ksdODnejdDw'

class ReusableForm(Form):
    userpath = TextField('Userpath:', validators=[validators.required()])

def actions(userpath):


@app.route("/", methods=['GET', 'POST'])
def userform():
    form = ReusableForm(request.form)

    #print(form.errors)
    if request.method == 'POST':
        userpath=request.form['userpath']
        userpath = str(userpath)
        dest = os.path.join(userpath) 


        if form.validate():
            actions(userpath)

            flash('Actions being completed on: {} {}'.format(userpath))

        else:
            flash('Error: All Fields are Required')

    return render_template('userform.html', form=form)

但是,我似乎不能这样指定路径。我不确定“/”的使用是否改变了功能,但是,当我将路径名直接放入python脚本时,一切都正常,因此我知道这与如何从表单中获取这些信息有关


Tags: 用户namefrombrimport路径formapp

热门问题