I have a problem with my code. I'm trying to create a docx generator with docxtpl and flask, but I can't get it to work. My intention is to download the file generated with the information acquired from the form.
Here is the code in app.py:
from docxtpl import DocxTemplate
from flask import Flask, render_template, request, redirect, send_from_directory, url_for, send_file
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST',])
def index():
# generate_report()
return render_template('index.html')
@app.route('/', methods=['GET', 'POST']) # When the user click to the "submit" button
def download_file():
if request.method == 'POST':
result = request.form
doc = DocxTemplate("templates/template_procuracao.docx")
context = {
'autor':result['autor'],
'nacionalidade':result['nacionalidade'],
'estadocivil':result['estadocivil'],
'identidade':result['identidade'],
}
doc.render(context)
doc.save()
return send_file('templates/output',
attachment_filename='teste.docx',
as_attachment=True)
if __name__ == '__main__':
app.run()
Here is the code in index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="{{ url_for('index') }}" method="post">
<input type="text" id="autor" name="autor" placeholder="AUTOR"><br>
<input type="text" id="nacionalidade" name="nacionalidade" placeholder="NACIONALIDADE"><br>
<input type="text" id="estadocivil" name="estadocivil" placeholder="ESTADOCIVIL"><br>
<input type="text" id="identidade" name="identidade" placeholder="IDENTIDADE"><br>
<a href="{{url_for('download_file')}}" type="submit">Submit</a>
</form>
</body>
</html>
after clicking the submit button nothing happens. 127.0.0.1 - - [06/Apr/2022 21:00:23] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [06/Apr/2022 21:00:33] "GET / HTTP/1.1" 200 -