1

I am using python 3 and flask in a webproject. I have multiple forms on a single page. Forms have a hidden_tag(), which is the csrf_token.

The problem is that the id's of all csrf_token are the same.

[DOM] Found 2 elements with non-unique id #csrf_token:

I could use ajax or simply use a new page for the form, but I'd like to understand whether there is a fix for that.

I tried to assign a custom id to each hidden_tag():

{{ form.hidden_tag(id="tag_1") }}

But it tells me:

TypeError: hidden_tag() got an unexpected keyword argument 'id'

So far I was not able to find a solution.

EDIT:

The error is displayed by the browser in the console:

enter image description here

EDIT:

I am trying to add the hidden_tag, so it will overwrite the original hidden tag, but I cant figure out how the value is created.

hidden_tag = HiddenField('EditUserPasswordHiddenTag', default="the value")

That is what I got:

<input id="hidden_tag" name="hidden_tag" type="hidden" value="the value">
Roman
  • 3,563
  • 5
  • 48
  • 104
  • Non-unique IDs are only a warning. How do you get this message? – Daniel Mar 28 '18 at 08:27
  • The browser displays the error in the console. – Roman Mar 28 '18 at 08:34
  • @Roman create a second csrf token field with a different ID in your form model and call hidden_tag(csrf_fieldname) for the csrk fields. http://flask-wtf.readthedocs.io/en/stable/api.html#flask_wtf.FlaskForm.hidden_tag – Rambarun Komaljeet Mar 28 '18 at 08:41
  • @Rambarun Komaljeet Its not working too, the value gets lost. – Roman Mar 28 '18 at 12:19
  • I voted to reopen, because the given link does not answer this particular question about duplicated IDs. None of the answers there mentions this problem. One of the answers (with `prefix=...`) solves it correctly, but as I said, without mentioning it, so the reader looking for an answer cannot know. – VPfB Jan 16 '22 at 15:10

0 Answers0