Django error "TemplateDoesNotExist", but "Using django.template.loaders.app_directories.Loader" File exists

Template loader finds template, but template is not loaded

TemplateDoesNotExist at /cardpayment/

cardpayment.html

Request Method:     GET
Request URL:    http://localhost:7000/cardpayment/
Django Version:     1.8
Exception Type:     TemplateDoesNotExist
Exception Value:    

cardpayment.html

Exception Location:     /home/sdr/sl/lib/python3.4/site-packages/django/template/loader.py in render_to_string, line 138
Python Executable:  /home/sdr/sl/bin/python
Python Version:     3.4.3
Python Path:    

['/home/sdr/sl/agryp',
 '/home/sdr/pycharm-4.0.6/helpers/pydev',
 '/home/sdr/sl/src/tastypie',
 '/home/sdr/sl/agryp',
 '/usr/local/lib/python34.zip',
 '/usr/local/lib/python3.4',
 '/usr/local/lib/python3.4/plat-linux',
 '/usr/local/lib/python3.4/lib-dynload',
 '/home/sdr/sl/lib/python3.4/site-packages']

Server time:    Tue, 5 May 2015 10:17:40 +0000
Template-loader postmortem

Django tried loading these templates, in this order:

    Using loader django.template.loaders.filesystem.Loader:
        /home/sdr/sl/agryp/templates/cardpayment.html (File does not exist)
    Using loader django.template.loaders.app_directories.Loader:
        /home/sdr/sl/agryp/agryp/templates/cardpayment.html (File exists) <=========== FILE EXISTS BUT NOT LOADED
        /home/sdr/sl/src/tastypie/tastypie/templates/cardpayment.html (File does not exist)
        /home/sdr/sl/lib/python3.4/site-packages/grappelli/templates/cardpayment.html (File does not exist)
        /home/sdr/sl/lib/python3.4/site-packages/django/contrib/admin/templates/cardpayment.html (File does not exist)
        /home/sdr/sl/lib/python3.4/site-packages/django/contrib/auth/templates/cardpayment.html (File does not exist)
        /home/sdr/sl/lib/python3.4/site-packages/oauth2_provider/templates/cardpayment.html (File does not exist)
        /home/sdr/sl/lib/python3.4/site-packages/selectable/templates/cardpayment.html (File does not exist)

      

As you can see, the loader can find the template.

The TEMPLATE_DIRS value in settings.py looks like this:

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',
                'allauth.account.context_processors.account',
                'allauth.socialaccount.context_processors.socialaccount',
            ],
        },
    },
]

      

I also tried moving the template to the project / templates directory, but the error persists.

The code is checked with 0 errors / warnings.

content of cardpayment.html

{% extends "base.html" %}
{% block title %}Card Payments over Phone{% endblock %}
{% block extrahead %}
    {% load selectable_tags %}
    {% include_ui_theme %}
{% endblock %}

{% block content %}
    <h1>Receive Card Payment</h1>
    <form name="paymentType"  id="paymentType" class="form-horizontal">
    <fieldset>
        <label>Check type of Customer
        <input type="radio" value="existing">Existing Customer<br />
        <input type="radio"  value="new">Nee Customer<br />
        </label>
    </fieldset>
    </form>

    <div class="row">
    <form class="form-horizontal">
        <table class="table-responsive table-bordered">
            {{ form.as_table }}
        </table>
    </form>
    </div>
{% endblock %}

      

+4


source to share


5 answers


I faced the same problem and this solution was to specify the templates directory (projects / templates) in the templates settings like this:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['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',
            "django.core.context_processors.media",
        ],
    },
},

      



]

+5


source


I am also using a config parameter DIRS

similar to you and I just ran into the same problem in Django 1.8.2. The problem seems to be related to how you are calling django.shortcuts.render

(or django.shortcuts.render_to_response

) in your views - are you using them?

TL; DR - try changing your call render_to_response

in your views so that you don't pass context_instance

or any obsolete argument like the one previously django.template.RequestContext

used.If you pass in context_instance

or any obsolete argument the django template loading code uses the obsolete path that doesn't support the parameter DIRS

and your template won't loaded - even if it exists on disk.

Here's a section of relevant code from the Django ( django/shortcuts.py

) source that shows how this situation occurs:



def render_to_response(template_name, context=None,
                       context_instance=_context_instance_undefined,
                       content_type=None, status=None, dirs=_dirs_undefined,
                       dictionary=_dictionary_undefined, using=None):
    """
    Returns a HttpResponse whose content is filled with the result of calling
    django.template.loader.render_to_string() with the passed arguments.
    """
    if (context_instance is _context_instance_undefined
            and dirs is _dirs_undefined
            and dictionary is _dictionary_undefined):
        # No deprecated arguments were passed - use the new code path
        content = loader.render_to_string(template_name, context, using=using)

    else:
        # Some deprecated arguments were passed - use the legacy code path
        content = loader.render_to_string(
            template_name, context, context_instance, dirs, dictionary,
            using=using)

    return HttpResponse(content, content_type, status)

      

If you follow this loader method render_to_string

(c django/template/loader.py

), you can see that your template will not be loaded if you pass any deprecated arguments render_to_reponse

in your views:

def render_to_string(template_name, context=None,
                     context_instance=_context_instance_undefined,
                     dirs=_dirs_undefined,
                     dictionary=_dictionary_undefined,
                     request=None, using=None):
    """
    Loads a template and renders it with a context. Returns a string.

    template_name may be a string or a list of strings.
    """
    if (context_instance is _context_instance_undefined
            and dirs is _dirs_undefined
            and dictionary is _dictionary_undefined):
        # No deprecated arguments were passed - use the new code path
        if isinstance(template_name, (list, tuple)):
            template = select_template(template_name, using=using)
        else:
            template = get_template(template_name, using=using)
        return template.render(context, request)

    else:
        # Some deprecated arguments were passed - use the legacy code path
        for engine in _engine_list(using):
            try:
                # This is required for deprecating properly arguments specific
                # to Django templates. Remove Engine.render_to_string() at the
                # same time as this code path in Django 2.0.
                if isinstance(engine, DjangoTemplates):
                    if request is not None:
                        raise ValueError(
                            "render_to_string doesn't support the request argument "
                            "when some deprecated arguments are passed.")
                        continue
                    # Hack -- use the internal Engine instance of DjangoTemplates.
                    return engine.engine.render_to_string(
                        template_name, context, context_instance, dirs, dictionary)
                elif context_instance is not _context_instance_undefined:
                    warnings.warn(
                        "Skipping template backend %s because its render_to_string "
                        "method doesn't support the context_instance argument." %
                        engine.name, stacklevel=2)
                elif dirs is not _dirs_undefined:
                    warnings.warn(
                        "Skipping template backend %s because its render_to_string "
                        "method doesn't support the dirs argument." % engine.name,
                        stacklevel=2)
                elif dictionary is not _dictionary_undefined:
                    warnings.warn(
                        "Skipping template backend %s because its render_to_string "
                        "method doesn't support the dictionary argument." %
                        engine.name, stacklevel=2)
            except TemplateDoesNotExist:
                continue

        if template_name:
            if isinstance(template_name, (list, tuple)):
                template_name = ', '.join(template_name)
            raise TemplateDoesNotExist(template_name)
        else:
            raise TemplateDoesNotExist("No template names provided")

      

+3


source


TemplateDoesNotExist... cardpayment.html

may mean that Django cannot find cardpayment.html

, or , it may mean that it can find cardpayment.html without problems, but cannot find in it {% include 'cardpayment_subsection.html' %}

.

Explanation:

I just got this error now, on a project that has been working for many years, and the other solutions here did not help me.

Mine cardpayment.html

was found by the template loaders but contained some stupidity
which meant it could not be rendered. The error messages mislead me saying that Django didn't know the file existed, when in fact it knew it existed, it just couldn't execute it successfully.

For a while, everything worked fine: it was cardpayment.html

rendered without problems.

views.py

def cardpaymentview(request):            
    return render_to_response("cardpayment.html", {
            "message": "Hi SO"
            }, context_instance=RequestContext(request))

      

All of a sudden, even though I didn't edit cardpayment.html

, I got this error:

TemplateDoesNotExist at / cardpaymentpage: cardpayment.html

Using loader django.template.loaders.app_directories.Loader:
folder/lib/python2.7/site-packages/django/contrib/admin/templates/cardpayment.html (File does not exist)
folder/project/app1/templates/cardpayment.html (File does not exist)
folder/project/app2/templates/cardpayment.html (File does not exist)
folder/project/paymentapp/templates/cardpayment.html (File exists)

      

The problem was that mine cardpayment.html

in turn has include

calling a different template:

{% include 'cardpayment_subsection.html' %}

      

I created an error by renaming the file cardpayment_subsection.html

to something_else.html

without editing cardpayment.html

, so the command include

did not go well.

But as you can see, the debug message did not indicate that the problem was the call cardpayment_subsection.html

(the file no longer exists).

Synopsis: If the other solutions don't work for you, try creating a test file cardpayment2.html

that just says "Hello world" and see if you can at least do it. The file may be cardpayment.html

readable, but there is an error in it. (In my case, I needed to change {% include '____.html' %}

to reference a file that actually exists!)

+3


source


Found a fix. I'm not sure why, but the new TEMPLATES directive (Django 1.8+) doesn't seem to work. I commented this out and delivered in the old style

TEMPLATE_DIRS = (
    '/home/sdr/sl/agryp/templates/',
 )

      

everything worked again. ARGH!

You will need to work out why the new TEMPLATES directive is causing me problems.

Thanks for trying to help the guys!

+2


source


Search for settings.py in your python project and add the following line. I hope this works

SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
   os.path.join(SETTINGS_PATH, 'templates'),
)

      

0


source







All Articles