J. Hesters
J. Hesters

Reputation: 14748

Django looks for templates in my .envs folder (+ Django Admin)

I have my folders set up like this:

~/projects/myproject/
~/.envs/myproject/

My app folders look like this:

myapp/
├── __init__.py
├── admin.py
├── forms.py
├── migrations/
├── models.py
├── tests/
├── templates/
├── urls.py
├── views.py

I just implemented a custom password reset view, because I wanted to use my own templates. The problem is, that the password reset view does not find my template. Here is my view:

class CustomPasswordResetConfirmView(views.PasswordResetConfirmView):
    success_url = reverse_lazy("accounts:password_reset_complete")
    template_name = "accounts/registration/password_reset_confirm.html"

And here is the error message that I get:

TemplateDoesNotExist at /auth/reset/MQ/set-password/
accounts/registration/password_reset_confirm.html.
Request Method: GET
Request URL:    http://localhost:8000/auth/reset/MQ/set-password/
Django Version: 1.11.13
Exception Type: TemplateDoesNotExist
Exception Value:    
accounts/registration/password_reset_confirm.html.
Exception Location: /Users/jan/.envs/myproject/lib/python3.6/site-packages/django/template/loader.py in select_template, line 53
Python Executable:  /Users/jan/.envs/myproject/bin/python
Python Version: 3.6.5
Python Path:    
['/Users/jan/Dev/Misc Tutorial/myproject',
 '/Users/jan/.envs/myproject/lib/python36.zip',
 '/Users/jan/.envs/myproject/lib/python3.6',
 '/Users/jan/.envs/myproject/lib/python3.6/lib-dynload',
 '/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
 '/Users/jan/.envs/myproject/lib/python3.6/site-packages']

How can I get Django to find my custom templates? From the error I can tell that he looks in my virtual environments.

I tried specifying the templates path like this:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            [os.path.join(BASE_DIR, 'templates')]
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

The code for this I got from the Django documentation. Unfortunately this doesn't help. If you have any idea how I can fix this, I would be very thankful!

(On a related note, I also tried to use django-admin instead of manage.py, because the official docs recommend it. But I couldn't set the DJANGO_SETTINGS_MODULE env variable so that it works. Maybe this problem is related.)

EDIT: The app for the account related stuff is called accounts. In it is a folder called templates, holding various other folders, one is called 'registration'.

Upvotes: 0

Views: 1807

Answers (2)

vishruth
vishruth

Reputation: 33

Django was looking for templates in my venv dir (my virtual environment dir) too, in my case, I solved it after I added my app in the settings file of the main project dir.

Basically in your app dir, there's an apps.py file which has a class named *YOURAPPNAME*Config. In my case, the app is accounts so the class is AccountsConfig. Then go into your main project dir, which is located in same directory as your manage.py file, open settings.py, scroll down to INSTALLED_APPS and add your class there. For me it would be accounts.apps.AccountsConfig

Upvotes: 2

J. Hesters
J. Hesters

Reputation: 14748

Kinda solved.

Circumvented the django-admin problem by using manage.py just as pydanny suggested.

And I just had my templates configured wrongly. Here is the new view I used:

class CustomPasswordResetConfirmView(views.PasswordResetConfirmView):
    success_url = reverse_lazy("accounts:password_reset_complete")
    template_name = "registration/password_reset_confirm.html"

I also put the template folder in my project root like this:

myproject
  accounts/
  ...
  myproject/
  templates/

The registration/ folder is now within this templates/ directory.

Together with the following settings everything works now, thank you for everyone that helped!

Settings:

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

Upvotes: 2

Related Questions