Introduction
This specific article will focus on the content of printing some display using HttpResponse object in Django-based application. There are some articles exist previously where they have relation on the content. Those articles are the one with the title of ‘How to Solve Blank Page Display when Executing HttpResponse Object in Django Application’ in this link. Basically, it is going to show how to actually print a display using just HttpResponse object in Django application. The base application exist from another article. The article has the title of ‘How to Solve Error Message : AttributeError: ‘str’ object has no attribute ‘get’ in Django’ and it exist in this link.
Django Application Configuration and Setting
Before moving on, the following below are the Django-based application description :
-
First of all, the existence of a Django-based application in the Django project’s folder. In this context, the application name is ‘sysapp’ as an example. It exist in the installed application list inside ‘settings.py’ file as file which has the responsible for defining the project configuration. Those configuration lines exist below :
# 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', ]
Read an 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 to create a Django-based application. The command for doing that is simple. Just execute ‘django-admin startapp app_name’ where in this example, ‘app_name’ is ‘sysapp’. Just choose any preferable name accordingly
-
Next, using the ‘urls.py’ file, define the URL for displaying the page. This is the content of the file :
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), ]
The most important line exist below :
path('', views.main),
-
Following after, it is including the ‘urls.py’ file of the application to the ‘urls.py’ file of the project. So, any URL address of the application is being accessed, the project recognize to process it further. Below is the content of the ‘urls.py’ file :
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')), ]
The most important from the above is in the following line :
path('sysapp', include('sysapp.urls')),
-
After that, just create a file with the name of ‘views.py’ for the application. It is defining the function for displaying or printing the page. The following 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("templates/sysapp/main.html")
-
Last but not least, it is for defining the HTML static page file with the name of ‘main.html’. In case of the ‘main.html’ file, the content is simple and it is exist as follows :
<html> <head> <title>Test</title> </head> <body> <h2>Hello World</h2> </body> </html>
-
So, the HttpResponse object will print it. But unfortunately, it is printing the actual string of the path. So, the following display will appear :
How to Print Page Display using HttpResponse object
In this part, the focus is just to show how to print the page display using HttpResponse object with the base application described earlier. In that case, how is the way to actually, do it ?. Well, another simple way is just to contain the content in a variable as follows :
from django.shortcuts import render, redirect, HttpResponse from django.template import loader # Create your views here. def main(request): content = """<html> <head> <title>Test</title> </head> <body> <h2>Hello World</h2> </body> </html>""" return HttpResponse(content)
So, instead of using file for defining the HTML content, it is using multiline string variable. Just pass the variable to the HttpResponse object as in the above snippet code, the following output display of the page will appear :