Flask在不同的文件中呈现两个表单,但只能获得第一个表单

时间:2017-05-07 20:50:01

标签: python flask wtforms

我正在尝试将csv文件上传到Mysql并选择了相应的列,但问题是,一旦我更改了路径,文件就会关闭。 所以我尝试在同一路径中渲染2个模板:第一个加载文件,第二个选择列。我只能访问第一个模板。 我正在使用env.is_submitted()测试第二个表单,但即使我没有提交它也会打印“已提交”

    @app.route('/upload', methods=['GET', 'POST'])
    def upload():
    form = UploadForm()
    global columnscsv, salessource
    if form.validate_on_submit():
       try:
         filename = secure_filename(form.csv.data.filename)
         file = form.csv.data

         if file and allowed_file(filename):                  
           print 'file_path'                   
           salessource = CSVSource(file, delimiter=',')   
           columnscsv = salessource.fieldnames 
           print columnscsv

       finally:  
           return render(salessource)  
    return render_template('upload.html', form=form)

    def render(salessource):
        env = envForm() 
        if env.is_submitted():
           print "submitted"
        return  render_template('form.html',columnscsv = columnscsv ,env =env)  

upload.html

    <html>
    <head>
     <title>Upload</title>
      </head>
     <body>
      <form method="post" enctype="multipart/form-data" >
      {{ form.hidden_tag() }}
      {{ form.csv }}
      <input type="submit">
    </form></body>
     </html>

form.html

        {% block body %}
        <form   name = "mapping" method="POST" enctype="multipart/form-data" >
           {{ env.hidden_tag() }}
            <table>
        {% for csv in columnscsv %}
        <tr> <td> {{ csv }}</td>
             <td><select name = "{{ csv }}" >       
             <option >year </option>
             <option >month</option>
             <option >day</option>       
             <option>reference</option>
             <option>designation</option>
             </select></td>
        </tr>
        {% endfor %}
        </table>
            <input type="submit" value="Submit" name = "submit" >
            </form>

        {% endblock %}

1 个答案:

答案 0 :(得分:0)

您的form.html只能在提交表单时呈现(您的render(salessource)在提交表单的检查范围内),所以我无法找到它不会以这种方式打印“已提交”。

如果你想渲染2个模板,我会发现像这样的工作:

  • 添加session['fileName'] = filename作为临时文件以了解文件是否已提交
  • 提交后重定向回自身
  • 检查是否存在session['fileName']以选择要呈现的模板

    @app.route('/upload', methods=['GET', 'POST'])
    def upload():
        form = UploadForm()
        global columnscsv, salessource
        if form.validate_on_submit():
            try:
                filename = secure_filename(form.csv.data.filename)
                file = form.csv.data
                session['fileName'] = filename
    
                if file and allowed_file(filename):                  
                    print 'file_path' 
                    salessource = CSVSource(file, delimiter=',')   
                    columnscsv = salessource.fieldnames 
                    print columnscsv
                redirect(url_for('upload'))
            except:
                raise
    
        if session.get('fileName') != None:
            render_template('form.html',columnscsv = columnscsv ,env=env)
        else:
            return render_template('upload.html', form=form)