Deploying to Apache: How do I tell Apache/mod_wsgi where my templates are?

I am trying to deploy a Django project to Apache 2.4 using mod-wsgi, on Windows 11 (and later Windows Server). The project works when I do python runserver, but it fails when I try to run httpd from Apache24/bin, reporting that:

TemplateDoesNotExist at /

My project is in the folder: C:/django/f2db_py
The project is called: f2db
The folder C:/django is set so all users have “full control” permission

My httpd.conf file has this in it towards the top:

LoadFile "C:/Python311/python311.dll"
LoadModule wsgi_module "C:/Python311/Lib/site-packages/mod_wsgi/server/mod_wsgi.cp311-win_amd64.pyd"
WSGIPythonHome "C:/Python311"

WSGIScriptAlias / C:/django/f2db_py/f2db/
WSGIPythonPath C:/django/f2db_py

<Directory C:/django/f2db_py/f2db>
Require all granted

And at the bottom:

Alias /static C:/django/f2db_py/static

<Directory /static>
    AllowOverride None
    Options All
    Require all granted

Here is the Template-loader postmortem

Django tried loading these templates, in this order:

Using engine django:

    django.template.loaders.filesystem.Loader: C:\Users\andyj\templates\home.html (Source does not exist)
    django.template.loaders.app_directories.Loader: C:\django\f2db_py\computers\templates\home.html (Source does not exist)
    django.template.loaders.app_directories.Loader: C:\Python311\Lib\site-packages\django\contrib\admin\templates\home.html (Source does not exist)
    django.template.loaders.app_directories.Loader: C:\Python311\Lib\site-packages\django\contrib\auth\templates\home.html (Source does not exist)

Where it should be looking is: C:\django\f2db_py\templates

The first line looks to be derived from where I run httpd from, C:\Users\andyj, and indeed if I run httpd from C:\django\f2db_py it finds the template. I want to run this as a service, so running from that folder is not a long term solution.

I am guess there is a setting for the project home directory, and it is defaulting to the current one. But I cannot work out what that is.

Apache itself doesn’t need to know where the templates are, Django does. (Templates are not static files and are not retrieved directly by Apache. They are rendered by Django.)

What is your TEMPLATES setting in your production settings file?

Prompted by your response, I have done a bit more investigating. I now realise that the issue is some generic templates I am using in templates. It finds the templates in each app, but not the ones in [root]/templates.

Here is TEMPLATES setting:

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
            'libraries': {

I now realise APP_DIR tells it to look in the templates folder for each installed app. Meanwhile DIRS tells it to look in templates, but that is relative to where it is called from, and that is the problem.

The simple solution is just to have an absolute path, and this works, so problem solved. However, it feels like there should be a better solution, given [root]/templates is a standard Django folder. Why does APP_DIR not have it look in there?

Actually, it’s not. It’s a common convention, but it’s neither required nor created by the default startproject command.

Additionally, the tutorial shows creating this entry as being relative to BASE_DIR, which should address the bulk of the issue you’re seeing here.

Ah, it was the BASE_DIR bit I was missing. Thanks a lot!