0

I have the following code in my template HTML page:

<form class="ui form" action="{{ url_for('download_pdf', text=original_text) }}" method="get">
    <button class="ui left floated submit button" type="submit">Export</button>
</form>

In this code, the original_text variable was passed to this template from within python and I am trying to pass it to another python function as:

@app.route("/download-pdf/<text>")
def download_pdf(text: str):
    data = text
    return render_template("success.html", data=data)

Now, this results in 404 not found error. It is trying to render it as:

https://page.net/download-pdf/He%20...long text here with spaces...?

If I do something like:

<form class="ui form" action="{{ url_for('download_pdf', text='hello') }}"

it seems to work.

Luca
  • 10,458
  • 24
  • 107
  • 234

1 Answers1

0

The text variable has spaces in it, and when it is passed in URL, the spaces are not encoded. You can pass the encoded text variable with quote to the template as a separate variable, like so:

from urllib.parse import quote

encoded_text = quote(original_text)
return render_template("template.html", encoded_text=encoded_text)

And use encoded_text in yout HTML template:

<form class="ui form" action="{{ url_for('download_pdf', text=encoded_text) }}" method="get">
    <button class="ui left floated submit button" type="submit">Export</button>
</form>
stfxc
  • 174
  • 6
  • I do not think it is this as when I replace it with action="{{ url_for('download_pdf', text='hello world', it works. – Luca Jan 11 '23 at 12:34
  • I also added a test with the encoded text but still has the same issues. – Luca Jan 11 '23 at 12:45