2

I am "trying" to develop a Django project with Bootstrap. Because each page requires the exact same Bootstrap code, it looks more logical to me to have one base template at project level.

Learning from this guide and this guide, here's the content of my project files:

settings.py

'DIRS': [(os.path.join(BASE_DIR, 'templates')),],

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.homepage, name='homepage'),
]

views.py

def homepage(request):
    return render(request, 'homepage/index.html')

index.html

{% extends 'base.html' %}

{% block title %}Hello from index.html{% endblock %}

{% block content %}
This is from the index.html
{% endblock %}

Now when I browse the homepage of site (localhost:8000) I get TemplateDoesNotExist error. When I look at the error details I see this:

Template-loader postmortem
Django tried loading these templates, in this order:

Using engine django:

django.template.loaders.filesystem.Loader: D:\Django Projects\omidshojaee.com\omidshojaee_com\templates\base.html (Source does not exist)

So Django is looking into the correct folder, and base.html is there (I can see it with my own eyes), so why Django cannot load it?

Edit: I should have explained the folder structure. It looks like this:

index.html inside omidshojaee_com(this is the project name)/homepage(this is the app name)/templates/homepage/

base.html inside omidshojaee_com/templates/

What I want is to have one base.html shared between all apps.

Omid Shojaee
  • 333
  • 1
  • 4
  • 20

1 Answers1

3

You have set 'templates' folder as the directory for your templates. So in your view should look like this

def homepage(request):
    return render(request, 'index.html')

And your index.html file should be in templates folder

  • And your base.html should be in templates folder – Nikita Prokaiev Oct 13 '20 at 12:20
  • Thanks but that's not what I want. I want index.html to be in the templates folder of its own app, and base.html to be in the templates folder of the project. Currently, index.html is in homepage(app name)/templates/homepage and base.html file is in omidshojaee_com(project)/templates/. My goal is to have one base.html shared between all apps. – Omid Shojaee Oct 13 '20 at 12:38
  • then in settings.py you have to add both directories `'DIRS': [ os.path.join(BASE_DIR, 'homepage/templates'), os.path.join(BASE_DIR, 'omidshojaee_com/templates'), ],` – Nikita Prokaiev Oct 13 '20 at 13:34
  • 1
    Thanks you were partially right. I addedd "omidshojaee_com/templates" and it solved the issue. – Omid Shojaee Oct 13 '20 at 14:02
  • Another question. Right now my DIRS is os.path.join(BASE_DIR, 'omidshojaee_com/templates') and it works. But what happens when I move it to the production server and 'omidshojaee_com' is not a valid path anymore? Is there any variable for project folder? – Omid Shojaee Oct 14 '20 at 16:21
  • Could you please tell a bit more brief about what you mean? – Nikita Prokaiev Oct 14 '20 at 16:22
  • Sorry for asking another question. To solve my problem I added PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) to my settings. This works but I'm not sure this is a proper approach? – Omid Shojaee Oct 14 '20 at 21:51