开发者

Django TemplateSyntaxError only on live server (templates exist)

I'm getting a strange error that only occurs on the live server. My Django templates directory is set up like so

  • base.html
  • two-column-base.html
  • portfolio
    • index.html
  • extranet
    • base.html
    • index.html

The portfolio pages work correctly locally on multiple machines. They inherit from either the root base.html or two-column-base.html. However, now that I've posted them to the live box (local machines are Windows, live is Linux), I get a TemplateSyntaxError: "Caught TemplateDoesNotExist while rendering: base.html" when I try to load any portfolio pages. It seems to be a case where the extends tag won't work in that root directory (???). Even if I do a direct_to_template on two-column-base.html (which extends base.html), I get that error. The extranet pages all work perfectly, but those templates all live inside the /extranet folder and inherit from /extranet/base.html.

Possible issues I've checked:

  • file permissions on the server are fine
  • the template directory is correct on the live box (I'm using os.path.dirname(os.path.realpath(__file__)) to make things work across machines)
  • files exist and the /templates directories exactly match my local copy
  • removing the {% extends %} block from the top of any broken template causes the templates to render without a problem
  • manually starting a shell session and calling get_template on any of the files works, but trying to render it blows up with the same exception on any of the extended templates. Doing the same with base.html, it renders perfectly (base.html also renders via direct_to_template)

Django 1.2, Python 2.6 on Webfaction. Apologies in advance because this is my 3rd or 4th "I'm doing something stupid" question in a row. The only x-factor I can think of is this is my first time using Mercurial instead ofsvn. Not sure how I could have messed things up via that.

EDIT: One possible source of problems: local machine is Python 2.5, live is 2.6.

Here's a traceback of me trying to render 'two-column-base.html', which extends 'base.html'. Both files are in the same directory, so if it can find the first, it can find the second. c is just an empty Context object.

>>> render_to_string('tw开发者_Python百科o-column-base.html', c)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/projectname/webapps/django/lib/python2.6/django/template/loader.py", line 186, in render_to_string
    return t.render(context_instance)
  File "/home/projectname/webapps/django/lib/python2.6/django/template/__init__.py", line 173, in render
    return self._render(context)
  File "/home/projectname/webapps/django/lib/python2.6/django/template/__init__.py", line 167, in _render
    return self.nodelist.render(context)
  File "/home/projectname/webapps/django/lib/python2.6/django/template/__init__.py", line 796, in render
    bits.append(self.render_node(node, context))
  File "/home/projectname/webapps/django/lib/python2.6/django/template/debug.py", line 72, in render_node
    result = node.render(context)
  File "/home/projectname/webapps/django/lib/python2.6/django/template/loader_tags.py", line 103, in render
    compiled_parent = self.get_parent(context)
  File "/home/projectname/webapps/django/lib/python2.6/django/template/loader_tags.py", line 100, in get_parent
    return get_template(parent)
  File "/home/projectname/webapps/django/lib/python2.6/django/template/loader.py", line 157, in get_template
    template, origin = find_template(template_name)
  File "/home/projectname/webapps/django/lib/python2.6/django/template/loader.py", line 138, in find_template
    raise TemplateDoesNotExist(name)
TemplateSyntaxError: Caught TemplateDoesNotExist while rendering: base.html

I'm wondering if this is somehow related to the template caching that was just added to Django.

EDIT 2 (per lazerscience):

template-related settings:

import os
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, 'templates'),
)

sample view:

def project_list(request, jobs, extra_context={}):
    context = {
        'jobs': jobs,
    }
    print context
    context.update(extra_context)

    return render_to_response('portfolio/index.html', context, context_instance=RequestContext(request))

EDIT: deleted dead links


ARGH: I should have posted the extends a long time ago. For posterity's sake and anyone else as dumb as me who manages to remember to breathe and figure out Google, here's the offender that works in Windows but not Linux.

{% extends "two-column-base.html "%}

instead of

{% extends "two-column-base.html" %}


{% extends "two-column-base.html "%} instead of {% extends "two-column-base.html" %}

Yep this caused me headache too !!! Up to to the point were I did a complete re-install of Django on the server.

Thxs

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜