So I am trying to open a template from...
But the template file is not shown as below...
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, "home/index.html")
Weird thing is that my webpage still works, and when I delete that function it does not.
Here is some additional Info...
settings.py
# Application definition
INSTALLED_APPS = [
'project_long_page',
'about',
'home',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
home/urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
]
Part 2 Question:
Added...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [str(BASE_DIR.joinpath('templates'))] # 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',
],
},
},
]
HTML Pages Within App (Don't mind errors in .HTML, I'll figure it out!)
base.html
Part 3: What is interesting is even though it says... Unresolved template reference '"main_header.html"' the code is still working and posted to the webpage. I have no idea how as it wasn't working before.
Part 4:
Added this in base.html
{% extends 'Headings/Headings1/base.html' %}
From settings..
'DIRS': [str(BASE_DIR.joinpath('templates'))] # BASE_DIR / 'templates'
Still not found. Yet the webpage is still working.
CodePudding user response:
Your templates folder should be within your app folder home (under about), otherwise Django won't find it. Check out https://django-project-skeleton.readthedocs.io/en/latest/structure.html for the proper file structure. My guess is the same could be said for the other folders withing your templates folder. You can have a templates folder in your root, but I don't think this is what you will need in your case.
To create a global template, you can have a template folder in your root 'testWebsite' directory like you already have. Then in your settings.py do the following:
# settings.py
TEMPLATES = [
{
...
'DIRS': [str(BASE_DIR.joinpath('templates'))],
...
},
]
What I usually do is create a base.html in each app, then in each of your html pages within the app you can add a line like {% extends "home/base.html" %}. And in each of your base.html files add {% extends "base.html" %}
Something like this (I didn't include all the files, just a sample):
testWebsite
├── about
│ └── templates
│ └── about
│ ├── base.html
│ └── about.html
├── home
│ └── templates
│ └── home
│ ├── base.html
│ ├── greet.html
│ └── index.html
├── project_long_page
│ └── templates
│ └── project_long_page
│ ├── base.html
│ └── project_long_page.html
├── templates
│ └── base.html
└── testWebsite
└── settings.py









