Introduction
So, this is another article which has a similar content or has a specific connection or relation with several previous articles. Those articles are ‘How to Solve Error Message ‘str’ object has no attribute ‘get’ when returning String Value to an Index Page of Django Application‘, ‘How to Solve Error Message PermissionError at / [Errno 13] Permission denied when changing index page in Django Application‘ and ‘How to Solve Error Message AttributeError at / ‘str’ object has no attribute ‘META’ when changing main page of Django Application‘.
As for the base Django application for an example, it is using the one exist in ‘How to Change Default Page of a Django Application‘. In this context, the trigger of the error exist in the ‘views.py’ file. It is a part of the source code exist in the ‘apps’ folder. The ‘apps’ folder is the main folder for the application. So, the following is the content of the ‘views.py’ which is triggering the error message :
from django.shortcuts import render # Create your views here. def index(request): render(request,"index.html")
On the other hand, the error appear in the main page when accessing the main URL of the Django application exist as follows :
ValueError at /
The view apps.views.index didn't return an HttpResponse object. It returned None instead.
Request Method: | GET |
---|---|
Request URL: | http://localhost:8000/ |
Django Version: | 4.1.1 |
Exception Type: | ValueError |
Exception Value: |
The view apps.views.index didn't return an HttpResponse object. It returned None instead. |
How to Solve Error Message The view apps.views.index didn’t return an HttpResponse object. It returned None instead
As for the solution of the error message, actually it is already exist in ‘How to Change Default Page of a Django Application‘. It is by changing the content of the ‘views.py’ which exist in the ‘apps’ folder. Specifically, it is an application folder which is part of the Django project. So, the following is the revision of the ‘views.py’ file :
from django.shortcuts import render # Create your views here. def index(request): return render(request,"index.html")
The reason for the error appearance is because of the usage of the render method as in the above part. In order to render a specific content which in this context itis an HTML file of ‘index.html’, it need to use a return keyword. It means, it will return an object of HttpResponse from the ‘render’ method. Using only ‘render()’ method will cause the Django application to be considered as to return nothing at all. Although render() method has the output of HttpResponse object. But without the ‘return’ keyword’, the Django application will missunderstand it into no HttpResponse object returned.