Advertisement
Guest User

Untitled

a guest
Jun 30th, 2019
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | None | 0 0
  1. from flask import Flask, redirect, url_for, render_template
  2. from flask_wtf import FlaskForm
  3. from wtforms import StringField, FieldList, FormField, SubmitField, HiddenField, Label
  4. from wtforms.validators import DataRequired
  5.  
  6. app = Flask(__name__)
  7. app.config['SECRET_KEY'] = 'SECRET_KEY-SECRET_KEY-SECRET_KEY'
  8.  
  9.  
  10. # subforms
  11. class SubForm(FlaskForm):
  12.     # how to handle hidden id that I can use to properly commit that on submit?
  13.     # entry_type_id = HiddenField()
  14.  
  15.     # validators for subforms don't work, but that's something I'll try to address later
  16.     content = StringField(validators=[DataRequired()])
  17.  
  18.     # I use custom __init__ set custom label for the field - or rather I try, as it doesn't work..
  19.     def __init__(self, custom_label=None, *args, **kwargs):
  20.         # not sure if safe - even just for the subform! #
  21.         # Without that, I get 'TypeError: argument of type 'CSRFTokenField' is not iterable' on main_form.validate_on_submit()
  22.         kwargs['csrf_enabled'] = False
  23.         FlaskForm.__init__(self, *args, **kwargs)
  24.  
  25.         if custom_label is not None:
  26.             self.content.label = Label(self.content.id, custom_label)
  27.             print(f'INIT // id: [{self.content.id}] // content.data: [{self.content.data}] // label: [{self.content.label.text}]')
  28.  
  29.  
  30. # main forms
  31. class MainForm(FlaskForm):
  32.     title = StringField('title')
  33.     entries = FieldList(FormField(SubForm))
  34.     submit = SubmitField('Post')
  35.  
  36.  
  37. @app.route("/test", methods=['GET', 'POST'])
  38. def test_route():
  39.     # the main form
  40.     main_form = MainForm(title='title')
  41.  
  42.     # sub forms, created before validate_on_submit()
  43.     sub_form_1 = SubForm(content='Default answer 1', custom_label='Question 1')
  44.     sub_form_2 = SubForm(content='Default answer 2', custom_label='Question 2')
  45.  
  46.     main_form.entries.append_entry(sub_form_1)
  47.     main_form.entries.append_entry(sub_form_2)
  48.  
  49.     if main_form.validate_on_submit():
  50.         for entry in main_form.entries.entries:
  51.             print(f'LOOP // id: [{entry.content.id}] // content.data: [{entry.content.data}] // label: [{entry.content.label.text}]')
  52.  
  53.         return redirect(url_for('test_route'))
  54.  
  55.     print(f'INSTANCE_1 // id: [{sub_form_1.content.id}] // content.data: [{sub_form_1.content.data}] // label: [{sub_form_1.content.label.text}]')
  56.     print(f'INSTANCE_2 // id: [{sub_form_2.content.id}] // content.data: [{sub_form_2.content.data}] // label: [{sub_form_2.content.label.text}]')
  57.  
  58.     return render_template('test_form.html', title='Test Form', main_form=main_form, legend='Test Form')
  59.  
  60.  
  61. if __name__ == '__main__':
  62.     app.run(debug=True)
  63.  
  64. # I have a whole list of issues here
  65. # 1) The main problem is that I can't read the subforms data in the .validate() when I submit the form
  66. # 2) The other thing is that I can't force the labels to show the custom values That I want to set dynamically
  67. # 3) I need to do more reading on handling csfr in subforms and how to work around that as well
  68. # 4) And the last one - how do I validate the subforms - for required fields, length, etc
  69.  
  70. # 1 & 2 is my main concern now and I have a feeling that the issues have the same root-cause
  71. # My gut feeling tells me that the broken element id is meaningful
  72. # ('content' for each subform string field, instead of indexed 'entries-0-content' - that I see on submit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement