Caught SyntaxError while rendering: invalid syntax
Question: I am creating an django app 'cms' with django1.1 via 《Python Web Development
with Django》. And I`m getting an "TemplateSyntaxError" error while accessing
admin page.

Here's the traceback:

    
    
    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
    

my views.py are :

    
    
    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())
    

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

And urls.py in cms:

    
    
    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"),             
    )
    


Answer: You have a small typo on line 18, where you're missing the Q class on the
second filter:

It should read:

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

