0

I'm thinking in organizing all my templates in inside my project: scolarte.

As suggested by this question:

What is the best location to put templates in django project?

If you can’t think of an obvious place to put your templates, we recommend creating a templates directory within your Django project (i.e., within the mysite directory you created in Chapter 2, if you’ve been following along with our examples).

But I need to call it from another app. The view is called but getting error:

TemplateDoesNotExist at /cuentas/ingreso/
scolarte/templates/scolarte/registration/signup.html

I even tried to put the full path to the template in project folder:

roles/views.py:

class SignUpView(TemplateView):
    template_name = 'scolarte/templates/scolarte/registration/signup.html'
    # don't work neither
    #template_name = 'templates/scolarte/registration/signup.html'
    #template_name = 'scolarte/registration/signup.html'
    #template_name = 'registration/signup.html'

roles/urls.py:

from django.urls import include, path
from .views import SignUpView, SellerSignUpView, ClientSignUpView


urlpatterns = [

    path('ingreso/', SignUpView.as_view(), name='signup'),
]

scolarte/urls.py

urlpatterns = [
    path('', include('core.urls')),
    path('cuentas/', include('roles.urls')),
    path('admin/', admin.site.urls),
]

My app is orgnized like this:

roles
    |_migrations
    |_templates
    ...
    |_urls.py
    |_views.py
scolarte   #project name
    |_templates
      |_scolarte
        |_registration
          |_signup.html
    |_setting.py
    |_urls.py

setting.py

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',
            ],
        },
    },
]

UPDATE 1:

enter image description here

roles app:

enter image description here

** roles app - view.py **:

enter image description here

UPDATE 2:

enter image description here

Omar Gonzales
  • 3,806
  • 10
  • 56
  • 120

1 Answers1

1

Your path to the template is incorrect by standard Django convention, but let me show you first how to fix it. What you'll want to do is make sure in settings.py you have these settings made. This is from a Django 3.0 fresh project creation.

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))    
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',
            ],
        },
    },
]

If you use those, and follow the suggested directory structure of:

scolarte
|_scolarte
 |_settings.py
 |_urls.py
  (etc..)
|_templates
 |_scolarte
  |_registration
   |_signup.html

Then you can use with this path:

template_name = 'scolarte/registration/signup.html'
SleighBoy
  • 501
  • 3
  • 12
  • I'm using all those parameters, tried with `template_name = 'scolarte/registration/signup.html'` without success. Please, see update 1. – Omar Gonzales Jan 11 '20 at 05:16
  • I'm used to put all templates in a "core" app not in project. I've said to do so, but saw a tutorial where the templates were in project folder. The convention is to use **appfolder** or **project folder**? – Omar Gonzales Jan 11 '20 at 05:19
  • According to this link, the project is the most appropiated place to put templates: https://stackoverflow.com/questions/1740436/what-is-the-best-location-to-put-templates-in-django-project – Omar Gonzales Jan 11 '20 at 05:25
  • Looking at update 1, you have ***templates*** in the same dir as settings.py, etc. If you create a project fresh from the Django cli tools it will put it one dir above that. They used to suggest it as you say, but the suggested convention changed at some point to create a top-level project directory then another identically named directory inside that with settings.py, the primary urlconf, etc. This was back when template settings were not as complex. That layout plays nicely with adding to the PYTHONPATH, but I am veering off-topic. – SleighBoy Jan 11 '20 at 05:37
  • Could you show this in you answer? `If you create a project fresh from the Django cli tools it will put it one dir above that`. I can change where this folder is as this is a new Django project. – Omar Gonzales Jan 11 '20 at 05:40
  • I checked it out, and I was wrong, the default when you use ***createproject*** does not use that DIRS setting, it has it empty. However the accepted answer on the linked SO question you mentioned follows the same convention that I have laid out with a ***templates*** dir in the project directory. – SleighBoy Jan 11 '20 at 05:56
  • I've updated my project structure to include a scolarte folder inside the project, as you suggested, but I'm getting: `ModuleNotFoundError: No module named 'scolarte.settings'` when running the server. Could you please se Update 2? – Omar Gonzales Jan 11 '20 at 06:14
  • You moved the settings which are just part of a module as far as Python treats it. Undo that, and just move "templates" out to the top-level of your project. So in that top-level you'd have ***roles***,***scolarte***,and ***templates***. That will fix it, and will make the settings I answered with work. manage.py wants to import _scolarte.settings_ and I am going to guess that the way the virtualenv is setup it will import from the project root. project in the sense of a VS Code project, not specifically Django. – SleighBoy Jan 11 '20 at 06:26
  • It solved the issue, but is not the answer as at the end I couldn't use the template dir inside the project **scolarte**, it is now at the same level as it and **roles**. – Omar Gonzales Jan 11 '20 at 06:33