渲染时捕获SyntaxError:语法无效

时间:2014-03-27 18:19:51

标签: python django django-admin

我正在使用django1.1通过“使用Django进行Python Web开发”创建一个django app'cms'。我在访问管理页面时遇到“TemplateSyntaxError”错误。

这是追溯:

TemplateSyntaxError at /admin/
Caught SyntaxError while rendering: invalid syntax (views.py, line 18)
Request Method: GET
Exception Type: TemplateSyntaxError
Exception Value:    
Caught SyntaxError while rendering: invalid syntax (views.py, line 18)
Exception Location: D:\Program Files\python\lib\site-packages\django\utils\importlib.py          in import_module, line 35
Python Executable:  D:\Program Files\python\python.exe
Python Version: 2.7.6

我的views.py是:

from django.shortcuts import render_to_response, get_object_or_404
from django.db.models import Q
from cms.models import Story, Category
from markdown import markdown
def category(request, slug):
    category = get_object_or_404(Category, slug = slug)
    story_list = Story.objects.filter(category=category)
    heading = "Category: %s " % category.label
    return render_to_response("cms/story_list.html", locals())
def search(request):
    if "q" in request.GET:
        term = request.GET['q']
        story_list = Story.objects.filter(Q(title__contains=term) | (markdown_content__contains=term))
        heading = "Search results"
    return render_to_response("cms/story_list.html", locals())
第18行是     story_list = Story.objects.filter(Q(title__contains = term)|(markdown_content__contains = term))

在cms中的urls.py:

from django.conf.urls.defaults import *
from cms.models import Story

info_dict = {'queryset': Story.objects.all(), 'template_object_name': 'story' }

urlpatterns = patterns('django.views.generic.list_detail',
    url(r'^(?P<slug>[-\w]+)/$', 'object_detail', info_dict, name="cms-story"),
    url(r'^$', 'object_list', info_dict, name='cms-home'),
)
urlpatterns += patterns('cmsproject.cms.views',
    url(r'^category/(?P<slug>[-\w]+)/$', 'category', name="cms-category"),
    url(r'^search/$', 'search', name="cms-search"),             
)

1 个答案:

答案 0 :(得分:1)

你在第18行有一个小错字,你在第二个过滤器上缺少Q类:

应该是:

story_list = Story.objects.filter(Q(title__contains=term) | Q(markdown_content__contains=term))