Django “TemplateDoesNotExist ” Error but “Using loader django.template.loaders.app_directories.Loader” File Exists

前端 未结 7 1957
我在风中等你
我在风中等你 2021-01-05 19:05

Template Loader finds the template but template is not loaded

TemplateDoesNotExist at /cardpayment/

cardpayment.html

Request Method:     G         


        
7条回答
  •  轮回少年
    2021-01-05 19:44

    TemplateDoesNotExist... cardpayment.html might mean Django can't find cardpayment.html, or it might mean it can find cardpayment.html no problem, but can't find some {% include 'cardpayment_subsection.html' %} within it.

    Explanation:

    I got this error just now, in a project that's been working for years, and the other solutions here didn't help me.

    My cardpayment.html was being found by the template loaders, but contained some nonsense which meant it could not be rendered. The error messages mislead me into thinking Django didn't know the file exists, when in fact it knew it existed, just couldn't successfully render it.

    For a while everything was working fine: cardpayment.html was being rendered without a problem.

    views.py

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

    Suddenly, even though I hadn't been editing cardpayment.html, I got that 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 my cardpayment.html in turn has an include calling another template:

    {% include 'cardpayment_subsection.html' %}
    

    I created the error by renaming the file cardpayment_subsection.html to something_else.html, without editing cardpayment.html, so that include command naturally failed.

    But as you can see, the debug message didn't indicate the problem was callling the cardpayment_subsection.html (file no longer exists).

    Summary: if other solutions don't work for you, try creating a test file cardpayment2.html that simply says "Hello world" and see if you can at least render that instead. Maybe the cardpayment.html file is being read, but has an error within it. (In my case, I needed to change the {% include '____.html' %} to refer to a file that actually does exist!)

提交回复
热议问题