2010-09-27 82 views
1

我得到一個TemplateSyntaxError,這隻發生在我的開發服務器上,但在本地的django測試服務器上正常工作。Django TemplateSyntaxError

這裏的錯誤

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 * 

回答

3

在你的urls.py中,通過url函數使用命名的url。

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

然後在您的模板中使用{% url my_view %}

如果沒有幫助,請粘貼您的urls.py - 也許這只是一些微小的語法錯誤。

+0

我認爲我的網址模式很好,因爲它在本地工作正常,我認爲如果我在urls.py文件中犯了一個語法錯誤,django會拋出一個不同的錯誤。 – imns 2010-09-27 20:49:29

+2

不一定。你可以有一個'url()',它可以很好地服務於頁面(轉發分辨率),但不能用'reverse()';正向和反向分辨率是相對獨立的。請粘貼具有'plan.views.profile'視圖的'urls.py'中的行。 – 2010-09-27 20:53:04

+1

嘗試ars的建議,如果它不起作用粘貼urls.py - 這就是我現在可以告訴的。 – bx2 2010-09-27 20:53:40

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文件是否有語法錯誤。

相關問題