Overview
Currently Aurora only supports WTForms for handling forms.
The WTForms is a third-party package, and you should have it installed on your environment.
If you don't have it yet, install it using the following command:
(venv) pip install WTForms
To see the WTForms documentation please visit the following address:
Using Forms
The first step to using Form classes is to import them into your controllers.
As an example, we want to import the created form in our Index
controller of our notes
app:
Now we need to add it to the index.html
view of the notes
app:
-
The
NOTES
here is a global variable for your app url.
-
The
form.csrf_token
, will creates a hidden input with the name of csrf_token
for you.
-
Using
form.title(class_="input-class")
syntax, you can add css classes to your inputs.
The above code will generate the following form for you:
You can still design custom forms of your choice, without using this syntax,
but remember to add the {{ form.csrf_token }}
at the beginning of your form tag.
Now, it's time to validate your form using WTForms validator:
Normally you return a rendered template, or a JSON object as the result.
Let's send the form with JavaScript and return a JSON response.
Add the following JavaScript Code to the main.js
of the notes app statics:
Then, update the controller:
Finally, try to submit the form, this time using JavaScript!