Introduction
Another article discussing about how to solve an error. The error exist as in the title of this article. Furthermore, the error appear upon execution an URL address of a Django-based application. For more information, this article has some connection with several previous articles. The first one is an article where it has the base application for the execution where the error in this article appear. It is an article with the title of ‘How to Solve Error Message django.core.exceptions.ImproperlyConfigured: No URL to redirect to. Provide a success_url.’ and it exist in this link. Another one is an article with the title of ‘How to Solve Error Message render() missing 1 required positional argument: ‘template_name’ in Django Application’ in this link. Both of the article are focusing on how to present or display static HTML file as an output of a Django-based web application.
But in this article, there is a different error appear as in the following line below :
ValueError: The view sysapp.views.main didn't return an HttpResponse object. It returned None instead.
For more information, the following is the image of the error appear upon accessing the URL of the application :
Django-based Application Configuration and Setting
Actually, before going on discussing about the error message and how to solve it, the following are the configuration and setting of the Django-based application :
-
First of all, it is the Django-based application with the name of ‘sysapp’. Read the article in this link with the title of ‘How to Create a New Application using Django Web-based Framework in Ubuntu Linux via Command Line’ for a reference. It is giving information about how to create a Django-based application. The command to do that is the same in every type of operating system.
-
Second, registering it as an installed application on the Django project. It exist in the ‘settings.py’ available in the project directory. The following configuration has that kind of purpose :
# 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', ]
The important part is the last line. That line is the line containing ‘sysapp’,.
-
Following after is to create the ‘urls.py’ file in the application folder. The following is the content of that 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), ]
-
Continue on, include that ‘urls.py’ file of the application to the ‘urls.py’ file of the project 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')), ]
-
Next, create the ‘views.py’ file for defining function with the name of ‘main’ which is responsible for displaying the output. That ‘main’ function exist as follows :
from django.shortcuts import render, redirect, HttpResponse from django.template import loader # Create your views here. def main(request): render(request, "templates/sysapp/main.html")
-
On the other hand, the file the name of ‘main.html’ has the following content :
<html> <head> <title>Test</title> </head> <body> <h2>Hello World</h2> </body> </html>
Solving the Error Message
The actual cause for triggering the error exist in the ‘views.py’. The conclusion came clear from the error log appear in the command line interface. That command line interface is where the execution for the application exist :
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 188, in _get_response self.check_response(response, callback) File "C:\programming\python\env\lib\site-packages\django\core\handlers\base.py", line 309, in check_response raise ValueError( ValueError: The view sysapp.views.main didn't return an HttpResponse object. It returned None instead.
In other words, the main function must return a HttpResponse object. So, it need a ‘return’ syntax. But what is the other syntax following the ‘return’ syntax ?. Off course a HttpResponse object and it happens to be suitable with the ‘render’ syntax. According to the Django documentation, it is a function which is returning the HttpResponse object as in this link. The following is the information about ‘render’ syntax :
render()
render
(request, template_name, context=None, content_type=None, status=None, using=None)¶- Combines a given template with a given context dictionary and returns an
HttpResponse
object with that rendered text.Django does not provide a shortcut function which returns a
TemplateResponse
because the constructor ofTemplateResponse
offers the same level of convenience asrender()
.
So, just change and revise the source a little bit. Modify the last line of the ‘main’ function. In the end, it will be as in the following content in order for the application to run properly :
from django.shortcuts import render, redirect, HttpResponse from django.template import loader # Create your views here. def main(request): return render(request, "templates/sysapp/main.html")