Template Issue of Views.py - python

I am new to Django. So I am trying to add a HTML template to the views.py but it is showing an error, it would be great if you would help me.
The full Error that I am seeing:
TemplateDoesNotExist at /
platform/home.html
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.0.8
Exception Type: TemplateDoesNotExist
Exception Value:
platform/home.html
Exception Location: C:\Users\Adeel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loader.py in get_template, line 19
Python Executable: C:\Users\Adeel\AppData\Local\Programs\Python\Python38-32\python.exe
Python Version: 3.8.3
Python Path:
['C:\\Users\\Adeel\\Google Drive\\Visual Studio\\Personal Portfolio\\Portfolio',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32\\lib',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32',
'C:\\Users\\Adeel\\AppData\\Roaming\\Python\\Python38\\site-packages',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages\\win32',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages\\win32\\lib',
'C:\\Users\\Adeel\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages\\Pythonwin']
Server time: Tue, 13 Oct 2020 06:14:44 +0000
views.py:
from django.shortcuts import render
def home(request):
return render(request, 'platform/home.html')
urls.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls.static import static
from django.conf import settings
from Platform import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='Home'),
]
settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
My HTML template that i am trying to add:
<h1>This is my project</h1>

Add the template to your directories
TEMPLATES = [
{
...
'DIRS': [
os.path.join(BASE_DIR, 'your/way/to/templates'),
],
...
]
After that
def home(request):
return render(request, 'home.html')
Hope I didn't miss anything
EDIT
You should have a separate folder with templates
and in urls.py
from . import views

Related

Django: Template does not exist

Please can you help me understand what I have done wrong here, I am sure it's a very simple problem but, I am new to django and especially the latest version...
I believe and as far to my research that I have routed my views to the best of my knowledge, please may you help identify my issue, I am getting an error:
"Page Not Found"
url.py from my app
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index')
]
views from my app
from django.shortcuts import render
from requests import request
# Create your views here.
def index(request):
return render(request, 'landing_page/index.html')
url from my project folder
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('/', include('comp.urls')),
path('admin/', admin.site.urls),
]
urls from my project
and then here is my project and template folders, I have attached an image for your ease of helping me:
In settings.py file, just add this:
os.path.join(BASE_DIR, 'templates')
EX:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], #Removed landing_page from here
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
#Removed libraries from here
},
},
]

TemplateDoesNotExist at / Exception Value: index.html django

welcome
i make a new project and i put index.html in templates dirctory and when i runserver i got the message TemplateDoesNotExist at / where the mistake i done
there is the views.py
from django.shortcuts import render, redirect
def index(request):
return render(request,'index.html')
there is the urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index,name='index'),
]
and there is the sitting.py
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent
TEMPLATES_DIR = os.path.join(BASE_DIR, 'templates')
STATIC_DIR = os.path.join(BASE_DIR, 'static')
the templates_dir and static_dir i add it after make project
just add TEMPLATES_DIR inside TEMPLATES in settings.py file.
Example:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR], #Added here
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
And see if it solves that error

Django: How to pass variables from view to a template

I'm having trouble passing a variable from view to an html file. This is what I did so far:
My root directory (where the manage.py file is) contains the following folders:
myapp
myproject
templates
myproject/urls.py looks like this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls'))
]
myproject/settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR, 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
myapp/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index')
]
templates/index.html:
<p>Hello {{ name }}</p>
When running the server locally, the only thing it shows is "Hello" without the name. What's wrong?
So let's start with views.py. When your urls.py calls your function in views.py, it's asking it to render a template.
Let's walk through a view function:
def index(request): the function the view
context = {'name':'Sol'} the key in the dictionary is what the HTML can call it and the value is what HTML gets back. So, when you do "{{ name }}" in HTML, it is replaced by Django with "Sol".
return render(request, "index.html", context) Making Django render it, request is the parameter from the function, "index.html" is the html file that Django is going to render, and context is the key + value pair that Django is going to use to replace.

(django)TemplateDoesNotExist at /

urls.py
from django.urls import path, include
urlpatterns = [
path('', include('calc.urls')),
path('admin/', admin.site.urls),
]
views.py
from django.shortcuts import render
def home(request):
return render(request, 'tmpl/home.html')
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'tmpl')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
calc/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home')
]
folder structure:
It looks like your post is mostly code; please add some more details.
It looks like your post is mostly code; please add some more details.
in your settings.py inside TEMPLATES do:
'DIRS': [os.path.join(BASE_DIR, 'top', 'tmpl')]
and in views.py
return render(request, 'home.html')

Django : Template not found/index.html

I am running a website using Django. There is no problem in login. When i logged in and click on some dashboard, it is showing "page not found"(404).
Views.py:
def index(request):
return(request,'obs_app/index.html')
Settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates/index.html')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR,],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'libraries': {
'get_by_index':'obs_app.templatetags.templatefilters',
'get_by_key':'obs_app.templatetags.templatefilters',
'get_dict':'obs_app.templatetags.templatefilters',
'get_items':'obs_app.templatetags.templatefilters',
'multiple':'obs_app.templatetags.templatefilters',
},
},
},
]
urls.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from obs_app import views
urlpatterns = [
url(r'index/',views.index , name = 'index'),
path('admin/', admin.site.urls),
path('', views.user_login, name='user_login'),
path('dashboard',views.obs_index, name='admin_dash'),
path('halls/active',views.obs_halls_active,name='active-halls'),
path('halls/pending',views.obs_halls_pending,name='pending-halls'),
path('febs/userlist', views.febs_user_list, name='febs-users'),
path('bookings/user', views.bookings_user, name='bookings-users'),
path('bookings/owner', views.bookings_owner, name='bookings-owner'),
path('cancellation/user', views.cancelled_user, name='cancelled-user'),
path('cancellation/owner', views.cancelled_owner, name='cancelled-owner'),
path('terms/obs', views.terms_conditions, name='terms-conditions'),
path('terms/febs', views.terms_febs, name='terms-febs'),
path('terms/febs/events', views.terms_febs_events, name='terms-febs-events'),
Error i am getting is :
Page not found (404)
The current path, index.html, didn't match any of these.
You have to render template and also fix the indentation
def index(request):
return render(request,'obs_app/index.html')
And also here you need to provide directory instead of file
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
Change:
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates/index.html')
To:
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
TEMPLATES = [
{
...
'DIRS': [], # DIRS defines a list of directories where the engine should look for template source files, in search order.
...
},
]
You might want to read: Support for template engines
Related part copied here:
DIRS defines a list of directories where the engine should look for template source files, in search order.

Categories