#Python #Shorts
One of the cool features in Django is allowing us to create forms using code, instead of defining them in our HTML template. This gives us the benefits of code reuse if we wanted to render the same form on multiple pages, or maybe have a base form and let other forms inherit from it. It also gives us the ability to validate input on the client side without doing much extra work.
To create such a form in Django, you can import "forms" from django and then define a new class that inherits from "forms.Form". Inside this class, you can define or more fields that you want to be rendered as a form, while giving them the type (for example, IntegerField), label and constraints, if you wish.
Then, you just create an instance of this new class and pass it into the HTML template via the context.
And in the HTML, all you have to do is render the form by using {{form}} syntax.
-------------------------------------------------------------------------------------
Links:
Blog: https://evgenyurubkov.com
Twitter: / evgenyurubkov
Instagram: / evgenyurubkov
Youtube: / @codevev
Watch video Django Forms - Python Web Development online without registration, duration hours minute second in high quality. This video was added by user codevev 08 July 2021, don't forget to share it with your friends and acquaintances, it has been viewed on our site 302 once and liked it 12 people.