#Beginner level - Template

16 messages Β· Page 1 of 1 (latest)

golden patio
#

Hello everyone,

I'm practicing on my first project on Django.
I've been searching for an answer for 2 hours now, I think I need some help (it seems not that complicated πŸ₯² ).

I'm using a template in my application and Django doesn't find it, I don't understand why.


My file views.py looks like this :

from django.http import HttpResponse
from django.template import loader

def users(request):
    template = loader.get_template('peoplebooks/base.html')
    ...
    return HttpResponse(template.render(context, request))

```

____

The project is build like this : 
πŸ“  PeopleBook
         πŸ“ PeopleBook
         πŸ“ peoplebooks *(the app)*
                 πŸ“ migrations
                 πŸ“ static
                 πŸ“ templates
                          πŸ“ peoplebooks
                                   < > base.html
                                   < > users_detail.html
                                   < > users_list.html

____

I have the error message : 
```
Django tried loading these templates, in this order:

Using engine django:

django.template.loaders.app_directories.Loader: /Users/.../PeopleBook/PeopleBook/.venv/lib/python3.12/site-packages/django/contrib/admin/templates/peoplebooks/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/.../PeopleBook/PeopleBook/.venv/lib/python3.12/site-packages/django/contrib/auth/templates/peoplebooks/base.html (Source does not exist)
```

____ 

I've tried different names...
I've tried to import os in the settings.py and added this : SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__)).

Sorry for the long post, if you have anything I could try/take a look at, it would be very helpful.
Thank you in advance !
midnight cliff
#

can you post the output of running: python manage.py diffsettings

#

also maybe post your settings.TEMPLATES value here as a whole, something seems misconfigured

golden patio
#

Thank you for your answer !
Here is the output :

AUTH_PASSWORD_VALIDATORS = [{'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator'}, {'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator'}, {'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator'}, {'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator'}]
BASE_DIR = PosixPath('/Users/.../PeopleBook/PeopleBook')  ###
DATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': PosixPath('/Users/.../PeopleBook/PeopleBook/db.sqlite3'), 'ATOMIC_REQUESTS': False, 'AUTOCOMMIT': True, 'CONN_MAX_AGE': 0, 'CONN_HEALTH_CHECKS': False, 'OPTIONS': {}, 'TIME_ZONE': None, 'USER': '', 'PASSWORD': '', 'HOST': '', 'PORT': '', 'TEST': {'CHARSET': None, 'COLLATION': None, 'MIGRATE': True, 'MIRROR': None, 'NAME': None}}}
DEBUG = True
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
INSTALLED_APPS = ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles']
#

and :

ROOT_URLCONF = 'PeopleBook.urls'  ###
SETTINGS_MODULE = 'PeopleBook.settings'  ###
SETTINGS_PATH = '/Users/.../PeopleBook/PeopleBook'  ###
STATIC_URL = 'static/'
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']}}]
TIME_ZONE = 'UTC'
WSGI_APPLICATION = 'PeopleBook.wsgi.application'
midnight cliff
#

please remove your SECRET_KEY from Discord and make sure to update your settings with a new, secret one 😬

#

I'm not aware what SETTINGS_PATH is supposed to do and if you think about it it would be weird to write into the settings file where the settings file is supposed to be found πŸ€”

#

env var, not a setting. you can remove SETTINGS_MODULE from your settings.py

#

if it's in there, maybe diffsettings is listing some env vars too? no idea

#

I have to leave for now unfortunately, maybe someone else can pick this up. there's certainly something wrong where your Django is trying to find your app's template files, should not look for them inside your .venv

golden patio
#

okay, thank you for your help !

golden patio
#

I found a solution after a day, omg. This learning is going to be a long road.

I imported os in settings.py.
And in templates part I wrote directly the template path in the directory :

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "peoplebookapp/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',
            ],
        },
    },
]```

I'm not sure it will work if I add a new app though.
tepid summit
#

@golden patio I hope you found a solution in the meantime, but it seems to me that you didn't add peoplebookapp to your INSTALLED_APPS (or peoplebooks, maybe you have renamed the app in the meantime?)

#

If you want templates to be loaded from app directories, or models to be loaded from models.py files, you have to add the containing module as an app to INSTALLED_APPS