Using HTML5 fields with WTForms

I use Flask to develop a web application. I initially did HTML forms manually, and then switched to using WTForms (this is an educational project, so I show each step of building a project).

I was a bit embarrassed trying to add HTML5 form fields like EmailField. I searched the WTForms documentation both online and I could not learn how to create a WTForm using HTML5 EmailField.

Then I installed this module https://pypi.python.org/pypi/wtforms-html5 , which enabled it, and it worked. But I was unhappy by adding an additional dependency, especially since it does not seem to be actively developing ( https://github.com/brutus/wtforms-html5 ).

Then I ended up on the github WTForms page and found that in fact there is support for all the new HTML5 fields, but these fields are not imported by default. https://github.com/wtforms/wtforms/blob/master/wtforms/fields/html5.py Therefore instead of using

from WTForms import EmailField 

How could one conclude from

 from WTForms import TextField 

Use instead

 from wtforms.fields.html5 import EmailField 

I previously used the wtforms-html5 module as follows

 from wtforms_html5 import EmailField 

So I changed all occurrences from wtforms_html5 to wtforms.fields.html5 , and my application works exactly as expected.

Ok, thanks for reading the entire background. Now for the questions:

  • Why is there not a single html5 field (EmailField, DateField, etc.) in the WTForms documentation?

  • Why these fields are not imported into WTForms by default, like others

  • Are these fields stable / intended for use?

  • What is the best practice for importing fields from WTForms?

In the "Text" field, I can use any of the following:

 from wtforms import TextField from wtforms.fields import TextField from wtforms.fields.simple import TextField 

But for EmailField I have to use

 from wtforms.fields.html5 import EmailField 

I would like to:

 from wtforms.fields import TextField from wtforms.fields import EmailField 

But this would require adding a line in the fields [__init__][1] file, which I would not want to do, as this is an educational project, and this will just confuse the students.

I'm looking for

  • Understanding why WTForms does not document or by default imports html5 fields
  • Any reasons to continue using the third-party wtforms-html5 module.
+5
source share
2 answers

Foreword, I am one of the authors and currently the main developer of WTForms

Before moving on to the question of why I should mention a very common template for people using WTForms, is to create your own module, which is all the bits you need in one namespace.

For example, in myapp/forms.py you do something like:

 from wtforms.fields import * from wtforms import widgets, Form as _Form from wtforms.fields.html5 import EmailField, class Form(_Form): """Awesome base form for myapp. Includes CSRF by default""" class Meta: csrf = True csrf_secret = 'secretpasswordhere' @property def csrf_context(self): return get_current_request().session # maybe add some methods you really wanted to have on your Form class 

You can use the above:

 from myapp.forms import Form, TextField, EmailField, ... class UserForm(Form): name = TextField(...) email = EmailField(...) 

or alternately with asterisk imports, for example from myapp.forms import *

You will notice that I also included a subclass of custom forms that sets up CSRF and provides a default CSRF context, which is not absolutely necessary. (By the way, Flask-WTF does a similar CSRF setup for you if you use a jar, but the point is to show how you can easily come with your own integration.)


Now why.

The main idea of ​​WTForms was to create a very simple and reliable structure that is well suited for most use cases, but extensible enough for people to provide related tools for working with specific use cases.

At the beginning of this decision was to include extensions to interact with various libraries. But this posed a problem in that it significantly increased the complexity and surface area of ​​the test, and also led to the release of WTForms in odd ways (Django just changed this thing, now you need to release new WTForms). Thus, in 2015, we decided to transfer all the extensions to our own packages in order to provide them with their own release schedule.

Instead of forcing people to use a single package that tries to do everything, it led to the creation of a truly large ecosystem with solid companion packages such as Flask-WTF , WTForms-Alchemy and WTForms-Django to name a few.

As to why HTML5 types are not documented; well, this is an omission. Also, some history: at some point, we switched the default output of WTForms from the output from the XHTML style to the HTML syntax; but kept the main outputs of the field. Thus, we got people who contributed to the return of XHTML forms, people contributing html5 fields, and people who wanted to use the default fields used HTML5 types, all after we guaranteed backward compatibility in the main version, which made our a hand.


I know that the long answer, and maybe the short way, is to say this: although WTForms usually works with most web frameworks, it is intended to be configured and customized for your purposes; do not connect and do not play for everything.

+5
source

The author of WTForms HTML5 is here. Just quickly led the project: it was "not supported" in a sense, that it worked for me, and I had nothing to add. Since vanilla WTForms started supporting all of this, I recommend using only regular WTForms.

If you need automatically generated rendering keywords for your widgets, the current versions of WTForms HTML5 have dropped all the fields and widgets that WTForms now also support and instead use the Meta class for forms that handles this.

+1
source

Source: https://habr.com/ru/post/1240271/


All Articles