Django templates rendering issue - python

I'm new to Django, trying my hand at creating a web app based on the self learn tutorial. i have created a app with name "travello" and project name is "travellproject", in the views i'am trying to render a html page.
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def homepage(request):
return render(request,'homepage.html')
i have created the "templates" directory under travellproj (please refer the directory structure below) also defined DIRS of template variable as below and urls.py as below.
"DIRS": [os.path.join(BASE_DIR,'templates')],
urlpatterns = [
path("",views.homepage),
path("admin/", admin.site.urls),
]
But I'm receiving a TemplateDoesNotExist error, please help.
Error
TemplateDoesNotExist at /
homepage.html
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 4.1.5
Exception Type: TemplateDoesNotExist
Exception Value:
homepage.html
Exception Location: C:\Users\Django\first_proj\lib\site-packages\django\template\loader.py, line 19, in get_template
Raised during: travello.views.homepage
Directory structure:
travello
|-views.py
travellProj
|-templates -- > homepage.html
|-urls.py
|-setting.py

Your templates folder should be at the root of your site. Let see how it should look like:
Project_folder
|-travelproj <-- Your site configuration folder
|-|-urls.py
|-|-wsgi.py
|-|-asgi.py
|-|-.......
|-travello <-- this is an application
|-|-views.py
|-|-urls.py
|-|-templates <-- templates for this app only
|-|-|-travello
|-|-|-|-template1.html
|-|-|-|-.......
|-|-.......
|-templates <- This is the templates folder for your site
|-|-homepage.html
|-|-..........
|-manage.py
|-requirements.txt
So in your case your templates folder is in your site configuration folder which is not a good practice. you have to move it one step higher.
Good practices tips:
At the root of your site (same level than manage.py) you have a templates folder which will contain the templates commons to all your applications.
In each application you have a folder templates/app_name which contain all the templates specifics for this application.
You have the same architecture for statics.

Inside templates create another new directory by your app name travello then keep the html file inside the travello directory
travello
| templates/travello/homepage.html
|-views.py
travellProj
|-urls.py
|-setting.py
Then views should be like
def homepage(request):
return render(request,'travello/homepage.html')

Related

how to fix template does not exist in Django?

I am a beginner in the Django framework. I created my project created my app and test it, it work fine till I decided to add a template. I don't know where the error is coming from because I follow what Django docs say by creating folder name templates in your app folder creating a folder with your app name and lastly creating the HTML file in the folder.
NOTE: other routes are working fine except the template
Please view the screenshot of my file structure and error below.
File Structure
ERROR
TemplateDoesNotExist at /blog/
Blog/index
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/
Django Version: 4.0.1
Exception Type: TemplateDoesNotExist
Exception Value:
Blog/index
Exception Location: C:\Python39\lib\site-packages\django\template\loader.py, line 19, in get_template
Python Executable: C:\Python39\python.exe
Python Version: 3.9.4
Python Path:
['C:\\Users\\Maxwell\\Desktop\\Django\\WebApp',
'C:\\Python39\\python39.zip',
'C:\\Python39\\DLLs',
'C:\\Python39\\lib',
'C:\\Python39',
'C:\\Users\\Maxwell\\AppData\\Roaming\\Python\\Python39\\site-packages',
'C:\\Python39\\lib\\site-packages',
'C:\\Python39\\lib\\site-packages\\win32',
'C:\\Python39\\lib\\site-packages\\win32\\lib',
'C:\\Python39\\lib\\site-packages\\Pythonwin']
Server time: Sun, 23 Jan 2022 12:04:18 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:\Users\Maxwell\Desktop\Django\WebApp\Blog\templates\ Blog\index (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Python39\lib\site-packages\django\contrib\admin\templates\ Blog\index (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Python39\lib\site-packages\django\contrib\auth\templates\ Blog\index (Source does not exist)
App/views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request,name):
return HttpResponse(f'Hello {name}')
def html(request):
return render(request,' Blog/index.html')
It seems that you render the template with Blog/index, but you need to specify the entire file name, so Blog/index.html and without a leading (or trailing) space:
def html(request):
return render(request, 'Blog/index.html')
if #Willem van Onsem answer didn't work
create a templates folder in the main directory 'in the same level as WebApp'
then inside it create folders for each app
in your settings.py you will find TEMPLATES option
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR,'templates')],#add this line
...
]
and when you want to use a template
return render(request,"template_folder/template_name.html")
I faced this situation too, and went through most of the answers here talking about the path, and editing the 'templates' function in settings.py.
In the end, it was because I forgot to add my app to the installed apps section in the settings.py file. I saw this in the docs at https://docs.djangoproject.com/en/4.0/intro/tutorial03/#write-views-that-actually-do-something

django reset-password views, NoReverseMatch error and also problems with templates

I need to include password reset function to my app, but always I have NoReverseMatch error. In addition, I can't view my own templates for password reset process. I created templates in registration folder in the same directory with other templates for my app: myapp/templates/registration
URLS.PY
from django.conf.urls import url, include
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
.....
url(r'^password_reset/$', auth_views.password_reset,
name='password_reset'),
url(r'^password_reset/done/$', auth_views.password_reset_done,
name='password_reset_done'),
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-
9A-Za-z]{1,20})/$',
auth_views.password_reset_confirm, name='password_reset_confirm'),
url(r'^reset/done/$', auth_views.password_reset_complete,
name='password_reset_complete'),
]
I have no custom views for password reset. Should I create views for it? Thanks in advance. Django version 1.11
As for the folder structure, you should follow django docs recommendation, wich states that you should create a subfolder in templates with the name of the app:
Template namespacing
Now we might be able to get away with putting our templates directly
in polls/templates (rather than creating another polls subdirectory),
but it would actually be a bad idea. Django will choose the first
template it finds whose name matches, and if you had a template with
the same name in a different application, Django would be unable to
distinguish between them. We need to be able to point Django at the
right one, and the easiest way to ensure this is by namespacing them.
That is, by putting those templates inside another directory named for
the application itself.
myapp/templates/myapp/registration #if this is a folder you call it in the views as 'myapp/registration/page.html'

Django: Passing a static html file into view produce TemplateDoesNotExist error

I have a django app and a view for the home page where I want to use a static html file for the homepage. My file structure is like this:
project/
stats (this is my app)/
urls.py
views.py
stats.html
(other files here)
In my views.py I have the following code:
from django.shortcuts import render_to_response
def index(request):
return render_to_response('stats.html')
When I run the urls, I just have the index page. I go to the stats page and I receive a TemplateDoesNotExist Error. Looking for an answer I tried to put stats/stats.html instead for the path, but I still get the same error. What is the correct way of doing this?
In your project directory, add a directory called 'templates' and inside the templates directory, create 'stats' directory. Put your HTML file in the stats directory.
Then change your settings file at the bottom from
TEMPLATE_DIRS = (
    os.path.join(SETTINGS_PATH, 'templates'),
)
To
TEMPLATE_DIRS = (
    os.path.join(BASE_PATH, 'templates'),
)
You can arrange your project like this
project/
stats (this is my app)/
urls.py
views.py
stats.html
templates/
stats/
stats.html
In your views.py, write this:
def index(request):
return render_to_response('stats/stats.html')
make sure there is 'stats' in the INSTALLED_APPS list in the settings.py.

Django index page shows ViewDoesNotExist at / Could not import mysite.views.home. Parent module mysite.views does not exist

I am new to django..
When I was working before 2 days..it was working properly..
Now django index page shows ..
ViewDoesNotExist at /
Could not import mysite.views.home. Parent module mysite.views does not exist.
my url.py contain
url(r'^$', 'mysite.views.home', name='home'),
please help me.
Where I did mistake??
Is the views.py that you are referring to in the project root? Error message says it can't find that file so check the path. If you moved the home view into an app folder, make sure you configured your settings file to include the app and point to the app folder as app_folder.views.home
Seems like a mistake in your views.py. Could you add the home view code?

deploy django project to webfaction and stuck with TemplateDoesNotExist

I'm trying to deploy django project to webfaction and stuck with the problem that the server does not see my templates folder
PROJECT_DIR = os.path.dirname((os.path.dirname((os.path.dirname(__file__)))))
TEMPLATE_DIRS = (
os.path.join(PROJECT_DIR,'templates'),
)
python path in httpd.conf :
python-path=/home/wadadaaa/webapps/promo_site/myproject:/home/wadadaaa/webapps/promo_site/lib/python2.7
And i have exception:
Exception Type: TemplateDoesNotExist
Exception Value: index.html
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/wadadaaa/webapps/promo_site/templates/index.html (File does not exist)
any ideas how to fix it?
When deploying to WebFaction I add the project's parent directory to the python_path:
python-path=/home/wadadaaa/webapps/promo_site:/home/wadadaaa/webapps/promo_site/myproject:/home/wadadaaa/webapps/promo_site/lib/python2.7
If you're using Django 1.5.x, a common way I "map" directory paths like TEMPLATE_DIRS, STATIC_ROOT, etc, is to use a function to compute those. This is especially useful if you work on more than one machine, or in a group, where the path to these files is going to vary per-developer:
# settings.py
import os
def map_path(directory_name):
return os.path.join(os.path.dirname(__file__),
'../' + directory_name).replace('\\', '/')
...
TEMPLATE_DIRS = (
map_path('templates'),
)
This is a convenient way to map your templates directory, given a project structure of:
my_app/
my_app/
__init__.py
settings.py
...
a_module/
__init__.py
models.py
templates/
layouts/
base.html
index.html
...

Categories