Django 模板语法错误

1 投票
2 回答
7859 浏览
提问于 2025-04-16 04:39

我在开发服务器上遇到了一个模板语法错误,这个错误只在我的开发服务器上出现,而在本地的Django测试服务器上运行得很好。

这是错误信息:

Caught SyntaxError while rendering: invalid syntax (urls.py, line 1)

还有HTML代码:

<li><a href="{% url plan.views.profile %}">Plan Details</a></li>

这是个人资料视图:

@login_required
def profile(request):
...

这是错误追踪信息:

Traceback:
File "/django/lib/python2.5/django/core/handlers/base.py" in get_response
  100.                     response = callback(request, *callback_args, **callback_kwargs)
File "/django/lib/python2.5/django/contrib/auth/decorators.py" in _wrapped_view
  25.                 return view_func(request, *args, **kwargs)
File "/django/myapp/plan/views.py" in profile
  121.             }, context_instance=RequestContext(request) )
File "/django/lib/python2.5/django/shortcuts/__init__.py" in render_to_response
  20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/django/lib/python2.5/django/template/loader.py" in render_to_string
  186.     return t.render(context_instance)
File "/django/lib/python2.5/django/template/__init__.py" in render
  173.             return self._render(context)
File "/django/lib/python2.5/django/template/__init__.py" in _render
  167.         return self.nodelist.render(context)
File "/django/lib/python2.5/django/template/__init__.py" in render
  796.                 bits.append(self.render_node(node, context))
File "/django/lib/python2.5/django/template/debug.py" in render_node
  72.             result = node.render(context)
File "/django/lib/python2.5/django/template/loader_tags.py" in render
  125.         return compiled_parent._render(context)
File "/django/lib/python2.5/django/template/__init__.py" in _render
  167.         return self.nodelist.render(context)
File "/django/lib/python2.5/django/template/__init__.py" in render
  796.                 bits.append(self.render_node(node, context))
File "/django/lib/python2.5/django/template/debug.py" in render_node
  72.             result = node.render(context)
File "/django/lib/python2.5/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/django/lib/python2.5/django/template/__init__.py" in render
  796.                 bits.append(self.render_node(node, context))
File "/django/lib/python2.5/django/template/debug.py" in render_node
  72.             result = node.render(context)
File "/django/lib/python2.5/django/template/loader_tags.py" in render
  139.             return self.template.render(context)
File "/django/lib/python2.5/django/template/__init__.py" in render
  173.             return self._render(context)
File "/django/lib/python2.5/django/template/__init__.py" in _render
  167.         return self.nodelist.render(context)
File "/django/lib/python2.5/django/template/__init__.py" in render
  796.                 bits.append(self.render_node(node, context))
File "/django/lib/python2.5/django/template/debug.py" in render_node
  72.             result = node.render(context)
File "/django/lib/python2.5/django/template/defaulttags.py" in render
  253.             return self.nodelist_false.render(context)
File "/django/lib/python2.5/django/template/__init__.py" in render
  796.                 bits.append(self.render_node(node, context))
File "/django/lib/python2.5/django/template/debug.py" in render_node
  72.             result = node.render(context)
File "/django/lib/python2.5/django/template/defaulttags.py" in render
  253.             return self.nodelist_false.render(context)
File "/django/lib/python2.5/django/template/__init__.py" in render
  796.                 bits.append(self.render_node(node, context))
File "/django/lib/python2.5/django/template/debug.py" in render_node
  72.             result = node.render(context)
File "/django/lib/python2.5/django/template/defaulttags.py" in render
  251.             return self.nodelist_true.render(context)
File "/django/lib/python2.5/django/template/__init__.py" in render
  796.                 bits.append(self.render_node(node, context))
File "/django/lib/python2.5/django/template/debug.py" in render_node
  72.             result = node.render(context)
File "/django/lib/python2.5/django/template/defaulttags.py" in render
  366.             url = reverse(self.view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "/django/lib/python2.5/django/core/urlresolvers.py" in reverse
  350.             *args, **kwargs)))
File "/django/lib/python2.5/django/core/urlresolvers.py" in reverse
  271.         possibilities = self.reverse_dict.getlist(lookup_view)
File "/django/lib/python2.5/django/core/urlresolvers.py" in _get_reverse_dict
  193.             self._populate()
File "/django/lib/python2.5/django/core/urlresolvers.py" in _populate
  173.                     for name in pattern.reverse_dict:
File "/django/lib/python2.5/django/core/urlresolvers.py" in _get_reverse_dict
  193.             self._populate()
File "/django/lib/python2.5/django/core/urlresolvers.py" in _populate
  162.         for pattern in reversed(self.url_patterns):
File "/django/lib/python2.5/django/core/urlresolvers.py" in _get_url_patterns
  243.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/django/lib/python2.5/django/core/urlresolvers.py" in _get_urlconf_module
  238.             self._urlconf_module = import_module(self.urlconf_name)
File "/django/lib/python2.5/django/utils/importlib.py" in import_module
  35.     __import__(name)

我检查过我的urls.py文件,没问题。第一行是:

from django.conf.urls.defaults import *

2 个回答

0

这里有一个可能的解决办法。你的视图文件的路径似乎是 /django/myapp/plan/views.py

File "/django/myapp/plan/views.py" in profile
    121.             }, context_instance=RequestContext(request) )

如果你的 Python 路径已经设置好包含 /django,那么你需要把你的模板/html 改成:

{% url myapp.plan.views.profile %}

如果不是这样的话,根据 bx2 的建议,你可能需要检查一下你的 urls.py 文件,看看里面有没有语法错误。

3

在你的urls.py文件中,使用带名字的链接,方法是通过url函数,比如这样。

urlpatterns = patterns('your_app.views',
    url(r'^somehiing/$', 'your_view_function', name='my_view'),
)

然后在你的模板中使用 {% url my_view %} 这个代码。

如果这样做还是不行,可以把你的urls.py贴过来,可能只是一些小的语法错误。

撰写回答