Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Web Development with Django

You're reading from  Web Development with Django

Product type Book
Published in Feb 2021
Publisher Packt
ISBN-13 9781839212505
Pages 826 pages
Edition 1st Edition
Languages
Authors (5):
Ben Shaw Ben Shaw
Profile icon Ben Shaw
Saurabh Badhwar Saurabh Badhwar
Profile icon Saurabh Badhwar
Andrew Bird Andrew Bird
Profile icon Andrew Bird
Bharath Chandra K S Bharath Chandra K S
Profile icon Bharath Chandra K S
Chris Guest Chris Guest
Profile icon Chris Guest
View More author details

Table of Contents (17) Chapters

Preface
1. Introduction to Django 2. Models and Migrations 3. URL Mapping, Views, and Templates 4. Introduction to Django Admin 5. Serving Static Files 6. Forms 7. Advanced Form Validation and Model Forms 8. Media Serving and File Uploads 9. Sessions and Authentication 10. Advanced Django Admin and Customizations 11. Advanced Templating and Class-Based Views 12. Building a REST API 13. Generating CSV, PDF, and Other Binary Files 14. Testing 15. Django Third-Party Libraries 16. Using a Frontend JavaScript Library with Django

6. Forms

Overview

This chapter introduces web forms, a method of sending information from the browser to the web server. It starts with an introduction to forms in general and discusses how data is encoded to be sent to the server. You will learn about the differences between sending form data in a GET HTTP request and sending it in a POST HTTP request, and how to choose which one to use. By the end of the chapter, you will know how Django's form library is used to build and validate forms automatically and how it cuts down the amount of manual HTML you need to write.

Introduction

So far, the views we have been building for Django have been one-way only. Our browser is retrieving data from the views we have written but it does not send any data back to them. In Chapter 4, Introduction to Django Admin, we created model instances using the Django admin and submitting forms, but those were using views built into Django, not created by us. In this chapter, we will use the Django Forms library to start accepting user-submitted data. The data will be provided through GET requests in the URL parameters, and/or POST requests in the body of the request. But before we get into the details, first let us understand what are forms in Django.

What Is a Form?

When working with an interactive web app, we not only want to provide data to users but also accept data from them to either customize the responses we are generating or let them submit data to the site. When browsing the web, you will most definitely have used forms. Whether you're logging in to your internet banking account, surfing the web with a browser, posting a message on social media, or writing an email in an online email client, in all these cases, you are entering data in a form. A form is made up of inputs that define key-value pairs of data to submit to the server. For example, when logging in to a website, the data being sent would have the keys username and password, with the values of your username and your password, respectively. We will go into the different types of inputs in more detail in the Types of Inputs section. Each input in the form has a name, and this is how its data is identified on the server-side (in a Django view). There can be...

The Django Forms Library

We've looked at how to manually write forms in HTML and how to access the data on the request object using QueryDict. We saw that the browser provides some validation for us for certain field types, such as email or numbers, but we have not tried validating the data in the Python view. We should validate the form in the Python view for two reasons:

  • It is not safe to rely solely on browser-based validation of input data. A browser may not implement certain validation features, meaning the user could post any type of data. For example, older browsers don't validate number fields, so a user can type in a number outside the range we are expecting. Furthermore, a malicious user could try to send harmful data without using a browser at all. The browser validation should be considered as a nicety for the user and that's all.
  • The browser does not allow us to do cross-field validation. For example, we can use the required attribute for inputs...

Validating Forms and Retrieving Python Values

So far, we have seen how Django Forms makes it much simpler to define a form using Python code and have it automatically rendered. We will now look at the other part of what makes Django forms useful: their ability to automatically validate the form and then retrieve native Python objects and values from them.

In Django, a form can either be unbound or bound. These terms describe whether or not the form has had the submitted POST data sent to it for validation. So far, we have only seen unbound forms – they are instantiated without arguments, like this:

form = ExampleForm()

A form is bound if it is called with some data to be used for validation, such as the POST data. A bound form can be created like this:

form = ExampleForm(request.POST)

A bound form allows us to start using built-in validation-related tools: first, the is_valid method to check the form's validity, then the cleaned_data attribute on the...

Summary

This chapter was an introduction to forms in Django. We introduced some HTML inputs for entering data onto a web page. We talked about how data is submitted to a web application and when to use GET and POST requests. We then looked at how Django's form classes can make generating the form HTML simpler, as well as allowing the automatic building of forms using models. We enhanced Bookr some more by building the book search functionality.

In the next chapter, we will go deeper into forms and learn how to customize the display of form fields, how to add more advanced validation to your form, and how to automatically save model instances by using the ModelForm class.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Web Development with Django
Published in: Feb 2021 Publisher: Packt ISBN-13: 9781839212505
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}