Introduction
This error is actually happening upon executing a certain line of source code in Django-based web application. Basically, there is an error upon accessing a certain page in the Django-based web application. The main goal is just to render a HTML file to print a string. But suddenly, it just generating an error as follows :
if response.get('X-Frame-Options') is not None: AttributeError: 'str' object has no attribute 'get' [26/Sep/2021 12:54:25] "GET /sysapp HTTP/1.1" 500 65396
It exist in the following display image :
Before getting on to the solution, the following is the actual condition of the Django-based application :
-
First of all,. there is an application in the Django-based project. In order to read about how to create an application in the Django-based framework, just read the article in this link. It is an article with the title of ‘How to Create a New Application using Django Web-based Framework in Ubuntu Linux via Command Line’. Although it is in Ubuntu Linux, the command is actually the same.
-
Next, the application registration so the Django-based project recognize it as an installed application is also done. Just add a line in the ‘settings.py’ file as follows :
-
Create a file with the name of ‘urls.py’ in order to store all available URL possible for the application. As a first step, just register the main URL address of the application. Fill the content of the file 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('', views.main), ]
-
Add the application’s URL definition file in to the project’s URL definition file as follows :
path('sysapp', include('sysapp.urls')),
So, the overall content is in the following line :
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')), ]
- Create a file with the name of ‘views.py’ in order to define the view or the presentation for the application. Just define a short function as follows :
from django.shortcuts import render # Create your views here. def main(request): return "templates/sysapp/main.html"
-
Last but not least, the following is the content of ‘main.html’ :
<html> <head> <title>Test</title> </head> <body> <h2>Hello World</h2> </body> </html>
How to Solve Error Message AttributeError: ‘str’ object has no attribute ‘get’ in Django
Well, turns out it generate an error as it exist in the previous part. So, in order to solve the problem, change the way to present or to display the view. It is obvious since it is pointing an error in the ‘views.py’ file. In order to display or present the HTML file correctly, just change only one of the line from the source code. So, the step exist as follows :
-
First of all, open the file with the name ‘views.py’.
-
Edit it by removing the last line of the source code from the ‘main’ function. The following is the actual source code of it :
from django.shortcuts import render # Create your views here. def main(request): return "templates/sysapp/main.html"
After removing that last line inside the ‘main’ function, it will be as follows :
from django.shortcuts import render # Create your views here. def main(request):
-
Add the following line :
return render(request, "templates/sysapp/main.html");
-
So, the final source will be in the following :
from django.shortcuts import render # Create your views here. def main(request): return "templates/sysapp/main.html"
- Execute it once more, the following display will appear :