How to Solve Blank Page Display when Executing HttpResponse Object in Django Application

Posted on

Introduction

This article is an article with a certain focus. The content is focusing on how to solve a blank page which is appearing. The appearance of the blank page is when there is an execution of a function in a ‘views.py’ file. This article has a certain relation with the previous article. That article is focusing on how to display a HTML static page in a Django-based application. In order to make the explanation has a foundation for further description, it is actually use a base application exist in another article. That is an article with the title of ‘How to Solve Error Message : AttributeError: ‘str’ object has no attribute ‘get’ in Django’ and it exist in this link.

Django Applicatation Condition

The following is the description of the Django application condition where the blank page appear :

  1. It has the name of ‘sysapp’ where it has been added to the ‘settings.py’ as an installed application of the project. Read an article for creating Django-based application in this link. It is an article with the title of ”. Continue on about the ‘settings.py’ file, it is the Django projects’ configuration file. These following configuration lines which is the definition of it :

    # Application definition
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'crispy_forms',
        'sysapp',
    ]
  2. It has several important parts consisting of several files. The first one is the file containing URL address definition of the application. It exist in ‘urls.py’ with the following content :

    from django.contrib import admin
    from django.urls import path, include, reverse
    from django.views.generic import RedirectView
    from . import views
    urlpatterns = [
        path('', views.main),
    ]
    
  3. After defining the URL address of the application, just add it to the URL address of the project. It exist in a file with the same name but different location. It is a file with the name of ‘urls.py’ in the project folder. Specifically, the configuration exist below :

    path('sysapp', include('sysapp.urls')),

    A complete one of the content exist as follows :

    from django.contrib import admin
    from django.urls import path, include, reverse
    from django.views.generic import RedirectView
    from . import views
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('sysapp', include('sysapp.urls')),
    ]
    
  4. Next is the ‘views.py’ file which is also exist in the application folder. It is responsible for processing the display of the page in this example. Below is the content of the file :

    from django.shortcuts import render, redirect, HttpResponse
    from django.template import loader
    # Create your views here.
    def main(request):
        return HttpResponse(request)
    
  5. Finally, the template file which is the HTML file. It exist with the name of ‘main.html’. The location is in the templates folder from the root project. It is where all the file for display interface available with another specific folder with the name of the application itself. The following is the tree structure :

    (env) C:\programming\python>tree myproject/
    Folder PATH listing for volume Windows
    Volume serial number is 00000028 E003:3593
    C:\PROGRAMMING\PYTHON\MYPROJECT
    ├───myapp
    │ ├───migrations
    │ │ └───__pycache__
    │ ├───templates
    │ │ └───myapp
    │ └───__pycache__
    ├───myproject
    │ ├───templates
    │ └───__pycache__
    ├───sysapp
    │ ├───migrations
    │ │ └───__pycache__
    │ ├───templates
    │ └───__pycache__
    └───templates
    ├───myapp
    ├───sysapp
    └───temporary
    (env) C:\programming\python>

    So, the content of the ‘main.html’ file exist as follows :

    <html>
      <head>
        <title>Test</title>
      </head>
      <body>
        <h2>Hello World</h2>
      </body>
    </html>
    

Solve Blank Page Display

Basically, the blank page display is because of the improper usage of the HttpResponse object in the Django-based application. As in the previous part available, there is a file which is using the HttpResponse object. That file is the ‘views.py’ file where the following is the actual content :

from django.shortcuts import render, redirect, HttpResponse
from django.template import loader
# Create your views here.
def main(request):
    template = loader.get_template('templates/sysapp/main.html')     
    return HttpResponse(request)

The above source code will produce a blank output. There is some way or solution to modify the above source code so that it will produce the correct output. The following is the actual modification of the source :

from django.shortcuts import render, redirect, HttpResponse
from django.template import loader
# Create your views here.
def main(request):
    template = loader.get_template('templates/sysapp/main.html')     
    return HttpResponse(template.render())

So, modify the last line of the main function. Change from the following line :

    return HttpResponse(request)

Into the following line :

    return HttpResponse(template.render())

The output will display the correct one which is the content of ‘main.html’ as follows :

How to Solve Blank Page Display when Executing HttpResponse Object in Django Application

Leave a Reply