Django搜索查询失败

时间:2011-12-27 19:10:50

标签: django search django-templates django-views

我已经在我正在处理的小网站上实现了此搜索功能,并且在执行搜索查询时遇到问题。这是追溯:

Traceback (most recent call last):
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/contrib/staticfiles/handlers.py", line 68, in __call__
    return self.application(environ, start_response)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/contrib/staticfiles/handlers.py", line 68, in __call__
    return self.application(environ, start_response)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 272, in __call__
    response = self.get_response(request)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/core/handlers/base.py", line 169, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/core/handlers/base.py", line 203, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/glosseyvilly/PycharmProjects/first_django_dev/apps/photos/views.py", line 113, in search_photos
    'search_query': search_query
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/shortcuts/__init__.py", line 44, in render
    return HttpResponse(loader.render_to_string(*args, **kwargs),
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/loader.py", line 188, in render_to_string
    return t.render(context_instance)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 123, in render
    return self._render(context)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 117, in _render
    return self.nodelist.render(context)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 744, in render
    bits.append(self.render_node(node, context))
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/debug.py", line 73, in render_node
    result = node.render(context)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/loader_tags.py", line 127, in render
    return compiled_parent._render(context)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 117, in _render
    return self.nodelist.render(context)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 744, in render
    bits.append(self.render_node(node, context))
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/debug.py", line 73, in render_node
    result = node.render(context)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/loader_tags.py", line 64, in render
    result = block.nodelist.render(context)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 744, in render
    bits.append(self.render_node(node, context))
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/debug.py", line 73, in render_node
    result = node.render(context)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/defaulttags.py", line 311, in render
    return self.nodelist_true.render(context)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 744, in render
    bits.append(self.render_node(node, context))
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/debug.py", line 73, in render_node
    result = node.render(context)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/defaulttags.py", line 227, in render
    nodelist.append(node.render(context))
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 915, in render
    resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 653, in resolve
    value = self._resolve_lookup(context)
  File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 692, in _resolve_lookup
    raise VariableDoesNotExist("Failed lookup for key [%s] in %r", (bit, current)) # missing attribute
TemplateSyntaxError: Caught VariableDoesNotExist while rendering: Failed lookup for key [user] in u"'uploaded_photos/Published/2011/December/21/image_5.jpeg'"

这是我的观点功能:

def search_photos(request):
    """

    """
    search_query = ''
    results = None
    if ('q' in request.GET) and request.GET['q'].strip():
            search_query = request.GET['q']
        photo_query = get_query(search_query, ['caption',])
        results = Photo.objects.filter(photo_query).order_by('-date_added')

    return render(request, 'photos/search.html', {
        'results': results,
        'search_query': search_query
    })

注意:追溯突出显示'search_query': search_query可能是错误,我不确定原因。

编辑:这是我的search.html模板的链接:gist.github.com/1524911

2 个答案:

答案 0 :(得分:1)

  1. 您需要将错误消息格式化为代码块。
  2. 特定错误发生在追溯结束时:在u“'uploaded_photos / Published / 2011 / December / 21 / image_5.jpeg'”中发现的密钥[user]失败_resolve_lookup。这是为模板提取变量值的例程。
  3. 我会仔细看看模板。特别注意可能正在进行奇怪查找的任何棘手的循环引用或模板标记(例如{% expr %})。让我们知道你的想法。

    <强>更新

    我的钱在线:

         {% phileo_widget result.user result %}
    

    result对象是否具有名为user的属性?

答案 1 :(得分:-1)

您的方法不应该将self作为其第一个参数吗?:

def search_photos(self):

因此,您使用以下命令访问request对象:

request = self.request