Introduction
This is an article where the main focus is to solve an error message. The error message appear on the process of rendering a template of HTML file. In this article, the process for rendering the HTML file template is using HttpResponse object. It is already exist in the previous several articles. The first one is an article with the title of ‘How to Present or Display a File using the HttpResponse Object in Django Application’. It is available in this link. Th second one is the detail from the previous one which exist in this link. It is an article with the title of ‘How to Print Page Display using HttpResponse Object in Django Application’. That article is focusing on the usage of HttpResponse object to print multiline string multiline which is connecting as one complete HTML file.
Basically, it is working to store the HTML file content in a muliline string variable as a parameter of the HttpResponse object to print it. But a more suitable way exist by using a rendered template as a parameter for the HttpResponse object to print or display a HTML file. There are already several articles discussing about it. Those are the article with the title of ‘How to Solve Blank Page Display when Executing HttpResponse Object in Django Application’ in this link. Furthermore, an article with the title of ‘How to Solve Only > Output as a Display when Return HttpResponse Template in Django’ in this link. But upon finding the way to do it, the following error appear :
The following is the snippet code which is triggering the error above :
def main(request): template = loader.get_template('templates/sysapp/main.html') return HttpResponse(template.render(request))
Solution
The solution for the above error is simple. It is basically exist because of improper usage of the HttpResponse object. The following is the actual error message from the command line output :
Internal Server Error: /sysapp Traceback (most recent call last): File "C:\programming\python\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner response = get_response(request) File "C:\programming\python\env\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\programming\python\myproject\sysapp\views.py", line 27, in main return HttpResponse(template.render(request)) File "C:\programming\python\env\lib\site-packages\django\template\backends\django.py", line 59, in render context = make_context(context, request, autoescape=self.backend.engine.autoescape) File "C:\programming\python\env\lib\site-packages\django\template\context.py", line 268, in make_context raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__) TypeError: context must be a dict rather than WSGIRequest.
Just take a look at certain part of the error message above :
File "C:\programming\python\myproject\sysapp\views.py", line 27, in main return HttpResponse(template.render(request))
The above line is focusing on the parameter of the render function. It is a context where it must be a dict rather than WSGIRequest. What is the WSGIRequest in the above line ?. It is the ‘request’ which is passed to the render function. It is actually a HttpRequest object. So, omit or erase the parameter in order to solve the problem. Overall, the revision of the script exist below :
def main(request): template = loader.get_template('templates/sysapp/main.html') return HttpResponse(template.render())
After accessing the page once more, the following output will appear :
The above result is obvious since the ‘main.html’ file which is being rendered as an output or display has the following content :
<html> <head> <title>Test</title> </head> <body> <h2>Hello World</h2> </body> </html>
The solution also appear in the article mentioned early. An article with the title of ‘How to Solve Blank Page Display when Executing HttpResponse Object in Django Application’ in this link.