Introduction
So, this article is also part of another previous article exist. It is an article where there is an attempt to change the default page of a running Django application. So, it exist in the article ‘How to Change Default Page of a Django Application‘. Unfortunately, upon changing the default page into a page which is printing just ‘Hello World’ in heading 1 style, an error occur. Actually, there is an article discussing about it in ‘How to Solve Error Message ‘str’ object has no attribute ‘get’ when returning String Value to an Index Page of Django Application. But on the process for changing the default page, there is another error appear. So, rather than using return HttpResponse, it is using the render method as follows :
from django.shortcuts import render # Create your views here. def index(request): render("<h2>Hello World</h2>","")
The source above is a revision from the original content of ‘views.py’ exist in the application folder as follows :
from django.shortcuts import render # Create your views here.
It is getting the error appearance as follows :
PermissionError at /
[Errno 13] Permission denied: 'C:\\project\\env\\lib\\site-packages\\django\\contrib\\admin\\templates'
Request Method: | GET |
---|---|
Request URL: | http://localhost:8000/ |
Django Version: | 4.1.1 |
Exception Type: | PermissionError |
PermissionError at / [Errno 13] Permission denied
There are two possible solutions for solving the error message. The first one is by implementing the solution exist in ‘How to Solve Error Message ‘str’ object has no attribute ‘get’ when returning String Value to an Index Page of Django Application‘. Another one is just by adding the template location which is the render method required. The error permission denied appear because the template folder does not exist. Currently, it is trying to access the template folder exist in ‘C:/project/env/lib/site-packages/django/contrib/admin/templates’.So, below are the steps for implementing the solutions :
-
Since the ‘templates’ folder does not exist, just create it. In this context, just create a folder with the name of ‘templates’ in the apps folder. The ‘apps’ folder is one of the Django application folder as follows :
C:\project\apps>dir Volume in drive C is Windows-SSD Volume Serial Number is CA30-19A4 Directory of C:\project\apps 09/06/2022 07:55 AM <DIR> . 09/06/2022 07:13 AM <DIR> .. 08/24/2022 10:02 AM 215 admin.py 08/24/2022 08:43 AM 146 apps.py 08/26/2022 02:19 PM <DIR> migrations 08/26/2022 02:05 PM 1,548 models.py 08/24/2022 08:43 AM 63 tests.py 09/06/2022 07:01 PM <DIR> templates 09/06/2022 08:03 AM 118 urls.py 09/06/2022 10:32 AM 258 views.py 08/24/2022 08:43 AM 0 __init__.py 7 File(s) 2,348 bytes 3 Dir(s) 49,026,093,056 bytes free C:\project\apps>
-
Furthermore, just create a file with the name of ‘index.html’ inside the ‘templates’ folder. It exist with the following content :
(env) C:\project\apps>tree . Folder PATH listing for volume Windows-SSD Volume serial number is CA30-19A4 C:\project\apps ├───migrations │ └───__pycache__ ├───templates └───__pycache__ (env) C:\project\apps>
-
Last but not least, just create a file with the name of ‘index.html’.Fill in the ‘index.html’ with the content below :
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h2>Hello World</h2> </body> </html>
-
Do not forget to revise the source code with the name of ‘views.py’ exist in the ‘apps’ folder which is the application folder. Below is the actual revision by changing or modify a line of source code inside of it :
from django.shortcuts import render # Create your views here. def index(request): return render(request,"index.html")
So, just modify the line :
render("<h2>Hello World</h2>","")
into the following line :
return render(request,"index.html")
- Finally, execute the Django application once more by accessing the URL of the main page. It will appear as in the following display :