jinja2 how to remove trailing newline

I think you can get rid of it using the whitespace control feature. Thus I would modify the endfor block to {% endfor -%}

See if that does it!


For those using Flask who arrive here, these lines did the trick for me:

app = Flask(__name__)
app.jinja_env.lstrip_blocks = True
app.jinja_env.trim_blocks = True

Change your loop to strip whitespace from the top AND bottom of the output (notice extra - at the for loop close):

{% for key, value in querystring.items() -%}
  {{ key }}: '{{ value }}'
{%- endfor %}

In my tests (using https://github.com/abourguignon/jinja2-live-parser), the - must come after the first {%, not before the last to achieve what you're asking for.

Docs: https://jinja.palletsprojects.com/en/latest/templates/#whitespace-control