Django - Tutorial - Setting IndexView but Getting TemplateDoesNotExist - django

I have searched through all the questions here already on TemplateDoesNotExist.
I have also been troubleshooting this for about 12 hours.
There is something I am not understanding. I am new to django.
I am following the following Thinkster tutorial:
Building Web Applications with Django and AngularJS
Here is the error:
Exception Location: C:\Users\Workstation333\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\loader.py in select_template, line 47
Exception Type: TemplateDoesNotExist
Exception Value:
index.html
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:\Users\Workstation333\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\admin\templates\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Workstation333\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\templates\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Workstation333\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\templates\index.html (Source does not exist)
This IndexView code I do not understand, and I believe is where the problem lies. The tutorial doesn't go over this code. I think this is what changes the index.html or how to find it. This is inside my views.py and is same project folder where my urls.py is located.
views.py
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic.base import TemplateView
from django.utils.decorators import method_decorator
class IndexView(TemplateView):
template_name = 'index.html'
#method_decorator(ensure_csrf_cookie)
def dispatch(self, *args, **kwargs):
return super(IndexView, self).dispatch(*args, **kwargs)
url.py
from django.contrib import admin
from django.urls import path, re_path, include
from rest_framework_nested import routers
from authentication.views import AccountViewSet
from HawkProject.views import IndexView
router = routers.SimpleRouter()
router.register(r'accounts', AccountViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^api/v1/', include(router.urls)),
re_path(r'^.*$', IndexView.as_view(), name='index')
]
partial settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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',
],
},
},
]
Please help, and Thank you.
Edit:
I added the following to the code, and I got it to not break right away,
but I don't think it is a solution, but a hack that will break, or is not maintainable. I added the index for the admin index inside Dirs like so:
'DIRS':
['C:/Users/Workstation333/AppData/Local/Programs/Python/Python36/Lib/site-
packages/django/contrib/admin/templates/admin'],
I couldn't find the other index files in the directories where it thinks they should have been. *notice the double admin in the directory above, not sure why that is. It also makes the "home" page take me directly to the admin page.

I haven't read through the tutorial you are doing but try doing this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(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',
],
},
},
]
As you can see, I think you are getting this error because your template directory 'DIRS' is blank and as such Django does not know where to search for 'index.html'. The 'templates' name can be changed to any name you want but just name it according to the folder which you keep your templates in within each of your apps.
Django Templates

Related

Customizing Django login template

I'm new to Django and I want to use my custom login page instead of the ugly looking django login template. How could I make it work? I think I need to edit the urls.py file and override the login template but I don't know how to do it. Thanks
urls.py
from django.urls import path
from django.contrib.auth.views import (
LoginView,
)
urlpatterns = [
path('login/', LoginView.as_view(template_name='login/login.html'), name='login'),
]
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(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',
],
},
},
]
LOGIN_URL = '/login/'
Now make a templates folder in the Base directory( where your manage.py is present) and in that folder make a new folder named login. Inside login folder make login.html
So the directory structure will be like this
-my_project
-templates
-login
-login.html
-manage.py
Now do whatever customization you want to do in login.html. You will get form object in context of the template, which is nothing but your login form.

How to Solve Django Error Message : TemplateDoesNotExist at

I am trying to do a project for a blog by django 3.1.2 , but unable to link the templates.I tried fixing it with **'DIRS': [os.path.join(BASE_DIR, 'templates'),]** I cannot find the logic miss. The template does exist and when I review the code, the query is performing correctly. I'm at a loss.
my setting
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(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',
],
},
},
]
my blog urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('', include('posts.urls')),
path('admin/', admin.site.urls),
]
my posts urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
my posts views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return render(request,'posts/index.html')
If your templates folder is in your application's directory
eg: app_name / templates / app_name /.html, then you do not need to add
'DIRS': [os.path.join(BASE_DIR, 'templates'),]'.
So, if your application name is my_posts, create a folder named templates in your application directory. Then again create a folder named my_posts(same as application name) in templates directory, and then inside it create all your html files. But remember in your views.py when you are rendering, you have to do it as such, 'my_posts/index.html'
You only need to add 'DIRS': [os.path.join(BASE_DIR, 'templates'),]' , if you have you template folder in your main/project directory.

Django template not found although it it exists in templates folder

Django version 2.2.6
I have the following folder structure and was expecting Django to find index.html based as it is placed in the default location under templates.
Am I doing something wrong here? Did Django stop looking up the template paths by default?
-app
|
settings.py
urls.py
...
templates
|
base.html
index.html
views.py
from django.shortcuts import render
# Create your views here.
def home_view(request):
return render(request, 'index.html')
urls.py
from django.contrib import admin
from django.urls import path
from .views import home_view
urlpatterns = [
path('', home_view, name='index'),
path('admin/', admin.site.urls),
]
You need to set the path of your templates folder in settings.py.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Leave DIRS blank and try again.
you are importing the home view page wrongly in the urls.py

Django base template location

I am creating a website using Django 2.0.7. I need to use a base template that is outside the purview of all applications, but can be inherited and used by templates in my other applications in the project.
I have edited my settings.py file appropriately (as per the documentation), but still when I go the root (i.e. home) page of my site, I get a blank page - can anyone explain why?
I have the following directory structure
myproj
----manage.py
----app1
----templates/
base.html
index.html
----static/
----myproj
__initi__.py
wsgi.py
settings.py
urls.py
views.py # <- I added this
The relevant parts of my settings.ini file looks like this:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['{0}/templates/'.format(BASE_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',
],
},
},
]
views.py:
from django.http import HttpResponse
def index(request):
return HttpResponse()
Why can't django find my index.html ?
Use 'DIRS': [os.path.join(BASE_DIR, "templates")] in your settings, and your view should return a object which knows what template to render, like this one:
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
There's also TemplateResponse(), SimpleTemplateResponse() and other objects you can return, depending on your needs.

TemplateDoesNotExist Django

I am new to Django. Getting this error (TemplateDoesNotExist ) when I refresh the page .
My code looks like this :
Project name : newsHtml
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', include('news.urls')),
]
I have created another directory called news where files looks like this :
views.py
from django.http import HttpResponse
from django.template import loader
def index(request):
template= loader.get_template('/home/index.html')
return HttpResponse(template.render(request))
# def index(request):
# return HttpResponse("<h1>its working<\h1>")
urls.py
from django.conf.urls import url
from. import views
urlpatterns = [
url(r'^$',views.index,name='index'),
]
when i uncomment the HttpResponse i am getting the output .
But not able to find why template is not working .
my directory structure looks like this :
First directory :
newsHtml->settings.py,urls.py,wsgi.py
Second directory:
news->templates/home/index.html,views.py,urls.py
I am not able to figure out what is missing . I am using Django 1.11.5.
For a start, why not use the Django shortcut.
from django.shortcuts import render
Your index view should look like so:
def index(request):
return render(request, "home/index.html")
I believe it's because you put a / before your home/index.html.
Also make sure this is in your settings:
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',
],
},
},
]
Furthermore, ensure your application is inside INSTALLED_APPS.