How to Correct Improper Usage of HttpResponse Displaying Template Object Name in Django Application

Posted on

Introduction

This is just another article showing the improper usage of HttpResponse. The reason is because it is actually printing the actual name of an object. It is obvious, since HttpResponse is accepting parameter which is going to be printed in the web page. This article has a connection with several previous articles discussing about similar topic. Those are an article with the title of ‘How to Solve Blank Page Display when Executing HttpResponse Object in Django Application’ in this link. Another one is an article in this link with the title of ‘How to Print Page Display using HttpResponse Object in Django Application’. Furthermore, the summary of those two articles in this link which is an article with the title of ‘How to Present or Display a File using the HttpResponse Object in Django Application’.

Improper Syntax of HttpResponse so it print the template object

Basically, HttpResponse object will accept a string parameter in a simple case. But in a more suitable way to print or display a page, it is always receive a parameter of a template. But receiving it without having another function modify the template will just ended in a wrong output. Instead of printing the template as a display in the page, it will automatically printing the template itself as an object. The following is the output of the execution :

How to Correct Improper Usage of HttpResponse Displaying Template Object Name in Django Application
How to Correct Improper Usage of HttpResponse Displaying Template Object Name in Django Application

The following source code is the one triggering the output above :

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, the last line of the above snippet code will print the object reference in string instead of printing the content of the template. In the previous article, there already has a solution for it. Just modify the ‘render’ into ‘render()’ since it is actually a method for rendering the template. It will render the template and pass it to the HttpResponse object. The final modification of the source code exist below :

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())

It will then display the following output :

How to Correct Improper Usage of HttpResponse Displaying Template Object Name in Django Application
How to Correct Improper Usage of HttpResponse Displaying Template Object Name in Django Application

It obvious since the ‘main.html’ file content is only as in the following scripts :

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

Leave a Reply