I was working on a Django project.
my urls.py
:
urlpatterns = [
path('', index, name='index')
]
my views.py
:
def index(request):
return render(request, 'index.html')
in my 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',
],
},
},
]
Till this point, all works perfectly, the template also rendered as it should. Now I approach to divide my settings.py
into 3 separate files for my production purpose like:
Settings (Folder)
| base.py
| development.py
| production.py
Here the base.py
contains almost all necessary settings, development.py
and production.py
inherit the base.py
and have only allowed host
and debug
values. development.py
and production.py
are also properly hooked up with manage.py
and wsgi.py
file of the project.
Now after this work it shows me an error like:
TemplateDoesNotExist at / index.html
I also try with
def index(request):
return HttpResponse("Hello, world. You're at the diagnosis index.")
It also works perfectly! maybe it only has problem with template/Base dir. Please suggest how can I fix this?
CodePudding user response:
Traditionally inside settings.py
we have address of BASE_DIR
which become changed after creating the file base.py
inside settings
folder. We need to modify it like:
BASE_DIR = Path(__file__).resolve().parent.parent.parent
The extra one .parent
helps it find the root
CodePudding user response:
In front the file name, add the app name:
def index(request):
return render(request, 'my_app/index.html')