Introduction
Basically, this article appear because of several previous articles. Those articles has the same purpose. It is to show how to present or display a file in Django-based application. The very first one is an article with the title of ‘How to Solve Error Message : AttributeError: ‘str’ object has no attribute ‘get’ in Django’ in this link. The next article after is the one with the title ‘How to Solve Error Message render() missing 1 required positional argument: ‘template_name’ in Django Application’ in this link. And the last one is an article with the title of ‘How to Solve Error Message ValueError: The view didn’t return an HttpResponse object. It returned None instead. in Django Application’ in this link.
Django Application Configuration and Setting
Before going on to the detail about several usage of the HttpResponse object in Django Application. The following is the actual condition of the Django-based application :
-
First of all, it is a Django-based application exist in a Django project. For detail information, read the article with the title of ‘How to Create a New Application using Django Web-based Framework in Ubuntu Linux via Command Line’ in this link. It is informing about how to create a Django-based application. Basically, just execute ‘django-admin startapp application_name’. In this example, the application name is ‘sysapp’.
-
Following after, registering the application name in the ‘settings.py’ file of the project. The following are the content of the configuration focusing on 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', ]
-
Next, it is the part of defining the files which is part of the application. The first one is the ‘urls.py’ for defining the URL address of the application. Create it in the root folder of the application’s folder. The following is the content of it :
from django.contrib import admin from django.urls import path from .views import ProductCreateView urlpatterns = [ path('', ProductCreateView.as_view()), ]
In order to recognize the URL address of the application, there is an additional step. That step is to add or to include the file in the ‘urls.py’ file which exist inside the root folder. The following line is the one for including it :
path('sysapp', include('sysapp.urls')),
Overall, the content of the ‘urls.py’ file in the project folder exist below :
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')),
-
Another file which is also part of the application is the file with the name of ‘views.py’. It is responsible for executing the logic of the application. In other words, it is holding the important part for presenting or displaying the output. The following is the content of the ‘views.py’ file :
from django.shortcuts import render, redirect, HttpResponse from django.template import loader # Create your views here. def main(request): return HttpResponse(request)
Focus on the last line of the main function. It is returning the HttpResponse object as it has to be. But the object itself does nothing at all. Just returning the HttpRequest object as a parameter of the HttpResponse object. It will just present or display blank page as follows :
How to Present or Display a File without Using the HttpResponse Object in Django Application
Several Usage of HttpResponse Object
There are several form of usage for the HttpResponse object in Django-based application. This article is trying to cover several of it. The goal is to give some additional information that helps when using the HttpResponse object. Starting from printing the string only, displaying only blank page and some other forms.
Printing String using HttpResponse Object
Basically, all of those articles are using HttpResponse object with a render function and then return it back so it will appear in the browser. But false usage of the HttpResponse object will not end with the expected result. The following is the appearance of printing the HttpResponse object instead of rendering it :
The above output will appear if there is a modification to the ‘views.py’ file as follows :
from django.shortcuts import render, redirect, HttpResponse from django.template import loader # Create your views here. def main(request): return HttpResponse("templates/sysapp/main.html")
According to the above output, the HttpResponse object can receive a string as the parameter. But can it receive a page as a parameter ?. Well, the answer is it actually can. Just pass a variable with a multiline string consisting of HTML tags representing one whole complete HTML page. Just read it in the previous article with the title of ‘How to Print Page Display using HttpResponse Object in Django Application’ in this link
Displaying the page from a rendered template using HttpResponse Object
Another form of output where there is nothing else appear as the output since it is only returning back the HttpRequest object back to the HttpResponse object as in the introduction part. The explanation for it has already exist in the introduction part. Just passing the HttpRequest object to the HttpResponse and return it back. There is an article exist previously discussing how to present or display a page using HttpResponse object. That article exist in the article with the title of ‘How to Solve Blank Page Display when Executing HttpResponse Object in Django Application’ in this link. In that article, it will actually solve the blank page by retrieving a template which is being rendered.