Phil
Phil

Reputation: 14681

How can I pass a variable for a jinja macro from the controller?

In a controller (//view) I perform an exists operation on the database. If the result is positive, I return the same URL and the form and flash a message.

When doing this, I'd like to render one of the fields (ie. form.email) with an extra variable set (that normally defaults to None) in order mark it as an erring field.

How can I do this using jinja templates and flask?

I am not asking for code but direction as I am unable to figure out the way to go.

I am working with Flask, WTForms and Jinja.

Thank you

Upvotes: 1

Views: 780

Answers (1)

Phil
Phil

Reputation: 14681

If your procedure encounters a (high level) operational error and you can not declare this as a validator within your WTForm objects, you might want to find another solution to mark that specific field as erring.

One way to do this can be using WTForms' helper class flags.

Ex:

form.field_name.flags.erring = True

Then when you are rendering your field, via a macro or not, you can check to see if erring flag is set to True.

Upvotes: 1

Related Questions