0

非常简单的代码,但情况很奇怪......

有一个带有这个片段的模板:

{% is_contest story as iscontest %}

其中 is_contest 是这个自定义模板标签:

@register.assignment_tag
def is_contest(obj):
    contesttype = ContentType.objects.get_for_model(Contest)
    return obj.source_content_type == contesttype

但是竞赛在 django_content_type 表中。

最奇怪的部分:这只是一个间歇性错误,似乎只是由模板标签引起的(我也在视图中进行了此检查,但那里没有错误)。

有任何想法吗?

编辑:完整追溯

Traceback (most recent call last):

  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

  File "/home/ubuntu/src/cbframe/commentableobjects/views/commentableobject_list.py", line 142, in commentablobject_list
    return render_to_response('commentableobjects-list.html', info, context_instance=RequestContext(request))

  File "/usr/local/lib/python2.7/dist-packages/django/shortcuts/__init__.py", line 20, in render_to_response
    return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 176, in render_to_string
    return t.render(context_instance)

  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 140, in render
    return self._render(context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 123, in render
    return compiled_parent._render(context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 281, in render
    return nodelist.render(context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 155, in render
    return self.render_template(self.template, context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 137, in render_template
    output = template.render(context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 140, in render
    return self._render(context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)

  File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 185, in render
    nodelist.append(node.render(context))

  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 1139, in render
    context[self.target_var] = func(*resolved_args, **resolved_kwargs)

  File "/home/ubuntu/src/cbframe/commentableobjects/templatetags/url_target_blank.py", line 27, in is_contest
    return obj.source_content_type == contesttype

  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 350, in __get__
    rel_obj = qs.get(**params)

  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 366, in get
    % self.model._meta.object_name)

DoesNotExist: ContentType matching query does not exist.
4

1 回答 1

1

一个问题可能是您正在相互比较 2 个实例,而这些在 Django 中应该是相同的,因为 Django 缓存了它们,这不能保证。

不知道如何/从哪里来source_content_type,但我会推荐这个:

return isinstance(obj, contesttype)

至于原来的问题,你有stacktrace吗?这个错误似乎有点奇怪。

您确定Contest没有在范围内的某处被覆盖吗? Contest可能会被模块中的其他代码覆盖,在这种情况下最有可能的嫌疑人是 aglobal Contest但这里似乎并非如此。

[编辑]

感谢堆栈跟踪,问题更加清晰。

这部分特别表明它在该obj.source_content_type部分中出错了:

return obj.source_content_type == contesttype

我认为某些对象source_content_type不再存在。

于 2012-08-28T23:36:04.023 回答