Quick Contact

    Python Tutorial
    Python Panda Tutorial
    Python Selenium
    Python Flask Tutorial
    Python Django
    Interview Questions & Answers

    Django Forms Handling & Validation

    Nowadays websites have become highly personalized resulting to some awesome applications for users. This personalization is an impact of user-data on the server. And, that data is highly valuable for you as it is used to provide a better user experience and more functionality. But, it also has some serious security consequences and legal issues. Therefore, when designing any user-input, keep in mind that user-data is highly valuable.

    So the question that should arise in your mind, how do we take user input and utilize the same? The answer is simple through forms.

    After completing this Django forms tutorial, you will have a great command over these concepts:

    • Forms
    • HTTP, GET & POST Method
    • Django Forms
    • Django Form Class
    • Django Form Validation

    What are Forms?

    Forms are a collection of HTML elements to take input from the user. In simple words, whenever you give any kind of instruction or input on web, that is known as a form.

    The input elements come inside < form>< /form> tags. It is collectively called as HTML form. All the websites use forms each having different use-cases.

    Some daily examples of forms can include:

    • Google search
    • Facebook posts and stories
    • Online registrations
    • Online Quizzes, tests, etc

    There are too many examples. Forms have become an essential part of web applications.

    We use them to structure and transmit user-data over HTTP. They provide all kinds of input options to be taken from the user.

    What is HTTP?

    HTTP is an acronym for HyperText Transfer Protocol. It is the set of rules which all the computers on a network follow to transmit data to other computers. All client/server architectures are based on this protocol.

    There are different methods to transmit data over HTTP. The 2 very popular methods among all of them are GET and POST.

    1. GET

      We use this method for user-data which will not be changing the state of the website. GET is a method of transmitting form data via URL.

      Google search is the best example of the same. We use this method for transmitting data which doesn’t make changes in the database.

      GET method comes in handy when we want to bookmark pages as our search query becomes part of the URL.

      GET method is often limited by the size of the URL. Anything more than URL size cannot be transmitted. That means we can only send text requests.

    2. POST

      This method is used for sensitive data and it changes the state of the website. POST is a method of transmitting data as collective structure over HTTP. It is sent as a different request from client alongside URL request, like cookies.

      POST data is not a cookie, it is just a different type of resource. This method is more secure than getting because the data is not in URL. The POST method also has a greater size limit, allowing for file uploads.

      This method is used on all the registration forms and quizzes.

    HTML Forms

    The < form> tag has two important attributes to be specified:

    1. Action

      This attribute specifies where to submit the form data. It can be left blank but in that case, the data will be sent to the same URL. Otherwise, you can specify a URL. This attribute just tells the form where to submit the data.

    2. Method

      This attribute specifies how to submit data. There are GET and POST values for the same. You should use capital values for this attribute.

      They will function as explained in the previous section.

    Django Forms Handling

    First, we have to understand how the server responds with forms and where do the forms fit in all the client/ server architecture?

    When a form is rendered in Django, these steps are involved:

      1. Render the Form in its Initial State

        When client requests form for the first time, the server responds with an empty form. The form fields can have initial values and help texts at that point. The initial state can consist data which may or may not be changed by the user.

      2. Receive the Form Data & Validate the Information

        When the users has a completely filled form, they submit it. The data received by the server needs to be validated.

        Here, validation means all kinds of things. Like,

        • If it is a valid email or not.
        • The passwords match with re-entered passwords or not.

    There are 2 different types of validations:

        1. Custom validations
        2. Built-in validations

    Their use depends on developer and project requirements.

    1. If the Received Data is not Valid, Response is a Bounded Form

      When you validate the form data, the function will either result in true or false. That means user either entered valid data or invalid data. In the latter case, we will have the server to return the form again, but this time form is bounded.

      A bounded form has user data in form fields. For example – when you sign-in on the website, at the time wrong password submission, the page does not reset your whole information rather it returns all the values in corresponding fields, filled by you and highlights only those fields where data is invalid.

      That response is a bounded form.

    2. If the Received Data is Valid, a Success Response will generate

      When your form data is validated, then response is a redirect or any success page. This part is also different for different websites.

      When the client/ browser requests for form for the first time, we receive a GET request, that’s how we check whether a request has been made for the first time. Then, the server responds with a default form, with initial values.

      In the second phase, the user completes and submits the form. This time the sumission was via the POST method. It will make the server recognize this request and perform validation checks.

      After that, you have to validate the form data . If the data is valid, we provide some kind of success response to our user. Otherwise, we respond with a bounded form stating that something was wrong in data, highlighting that particular field of data.

      Again, this process is repeated until the data becomes validated.

    Django Form Class

    Forms are implemented via Django form class. Some built-in tasks which Django provides you are:

    • Rendering Form
    • Form Validation

    And there are many more but we will be covering these 2 in our tutorial as these are the major ones.

    Django form class is very similar to the Django models class. Django form class provides you lots of functionality with forms and their validations. This class has many built-in forms which you can use directly to render HTML. You can also define forms, just like models and render the same.


    Apply now for Advanced Python Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.