How to Print Form POST Variable in Django Application

Posted on

Introduction

There is a need to print the content of a submitted form using POST method. This article has a connection with the previous article. That article is an article with the title of ‘How to Solve Error Message when cannot submit form in Django Application’ and it exist in this link. The following is the ‘views.py’ file containing the method for processing a simple form as follows :

def createRoom(request):
    form = RoomForm()
    if request.method == 'POST':
       form = RoomForm(request.POST)

The above script is just a snippet code from the ‘views.py’ which is a specific function. It is a function where it is displaying the form and also processing whatever submitted form action with the POST method is firing at it. The POST method request will be catched and then be passed to a form with the name of form and then executed for further process. So, there must be a way to print whatever is being fired and whatever is being catched by the form. It is by using a specific command, syntax or feature which is available either in Django or Python.

Printing the Form POST variable in Django Application

In order to do or to achieve the one exist in the title of this article is by trying several attempts. Those are in the following :

  1. The first trial is by using a logging feature. The following is the attempt for using the logging feature :

    import logging
    
    def createRoom(request):
        logging.basicConfig(level=logging.WARNING)     
        logger = logging.getLogger('apps')
        form = RoomForm()
        if request.method == 'POST':
            form = RoomForm(request.POST)
            logging.info(form)
            logging.info("Submitted Form")
    

    Apparently, the above script for printing the form does not giving the results.

  2. The second one is giving a shot by printing the variable sent from a form in Django-based application using just the print syntax as follows :

    import logging
    
    def createRoom(request):
        logging.basicConfig(level=logging.WARNING)     
        logger = logging.getLogger('apps')
        form = RoomForm()
        if request.method == 'POST':
            form = RoomForm(request.POST)
            print("Print : ",request.POST)
            logging.info(form)
            logging.info("Submitted Form")
    

    The following is the output of the above execution of the createRoom form :

    [01/Feb/2022 20:17:07] "GET /create-room/ HTTP/1.1" 200 2597
    Print : <QueryDict: {'csrfmiddlewaretoken': ['9TtCoxMyMEAuh2eTfn1XP4bN1EdMZDb8ZKHKzssFlTaiUzIWhwvlrwXVyGLIWTYk'], 'host': ['3'], 'topic': ['1'], 'name': ['Beginning Python'], 'description': ['Start learning python for beginner']}>
    [01/Feb/2022 20:17:40] "POST /create-room/ HTTP/1.1" 302 0
    [01/Feb/2022 20:17:40] "GET / HTTP/1.1" 200 3521
    

    So, the last attempt by using just ‘print’ syntax is very suitable in order to be able to do it.

Leave a Reply