代码之家  ›  专栏  ›  技术社区  ›  Swantewit

我的第一个flask应用程序-如何在没有数据库的情况下将html输入传递给python?

  •  0
  • Swantewit  · 技术社区  · 4 年前

    我创建了一个应用程序,可以将所选文件夹中最大的文件移到新文件夹中进行检查。我试着用django为它创建一个接口,当我问我的朋友(他精通python)这个架构时,他说我其实不需要数据库,所以我应该试试flask。 在一个flask教程中,我发现这个家伙实际上在使用SQLAlchemy并正在创建一个数据库。如何将HTML输入传递到python数据库? 我已经创建的纯python应用程序在没有任何数据库的情况下运行良好。

    HTML输入部分:

    {% block body %}
      <h1>Your computer can run faster today</h1>
      <p>You just need to remove the largest files from some of your folders. It's easy with this small project. Enjoy :)</p>
      <form>
        <label for="fname">Path to the folder you want to remove largest files from:</label><br>
        <input type="text" id="fname" name="fname"><br>
        <label for="lname">Path to the folder you want to put your removed files for inspection</label><br>
        <input type="text" id="lname" name="lname">
      </form>
    {% endblock %}
    

    Python输入部分:

    print("Type a path to the folder you want to remove the largest files from") 
    path1 = os.path.abspath(input())
    print("Type a path to create a folder for the largest files to inspect them before removing")
    path2 = os.path.abspath(input())
    path3 = path2 + '/ToBeRemoved'
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Simeon Nedkov    4 年前

    实现这一点的最基本的Flask应用程序如下所示。

    from flask import Flask, request, render_template
    app = Flask(__name__)
    
    @app.route('/')
    def remove_large_files():
        if request.method == 'POST':
            path_from = request.form.get('fname')
            path_to = request.form.get('lname')
    
            if path_from and path_to:
                # process your files
                
                # return a response
    
        return render_template('template.html')
    

    替换 template.html 通过包含表单的模板。

    参考 Request object docs 更多信息。