0

升级到 Django 1.5 后,我遇到了一个非常奇怪的错误。当我在激活 Django 调试工具栏的情况下访问我的 ClientDetailView 时,出现以下错误:

Traceback:
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  187.                 response = middleware_method(request, response)
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/debug_toolbar/panels/template.py" in process_response
  118.                     pformat(k(self.request))) for k in get_standard_processors()
File "/Users/mirkocrocop/workspace/upstream_backend/my_auth/context_processors.py" in extended_auth
  13.         user_groups = [g['name'] for g in tmp]
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/query.py" in _result_iter
  123.                 self._fill_cache()
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/query.py" in _fill_cache
  927.                     self._result_cache.append(next(self._iter))
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/query.py" in iterator
  1004.         for row in self.query.get_compiler(self.db).results_iter():
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in results_iter
  775.         for rows in self.execute_sql(MULTI):
File "/Users/mirkocrocop/.virtualenvs/upstream_backend/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  840.         cursor.execute(sql, params)

Exception Type: InternalError at /clients/5/
Exception Value: current transaction is aborted, commands ignored until end of transaction block

…我知道有时我必须停用它才能查看真正的错误是什么,但现在如果我停用 DDT,错误就会完全消失。它不应该影响生产服务器,但它仍然很烦人。

这是有问题的观点:

class BaseSingleClient(LoginRequiredMixin,
                       generic.detail.SingleObjectMixin):

    def get_context_data(self, **kwargs):
        '''
        Add the current client object to the context.
        This will be used in the template
        '''
        context = super(BaseSingleClient, self).get_context_data(**kwargs)
        try:
            self.client = self.kwargs['client_id']
        except KeyError, e:
            self.client = self.kwargs['pk']
        context['client_obj'] = UpstreamClientModel.objects.get(pk=
                                                                self.client)
        return context


class ClientDetailView(BaseSingleClient, generic.DetailView):
    model = UpstreamClientModel
    template_name = 'clients/client_detail.html'

以及发生错误时正在执行的 SQL:

DEBUG (0.004) SELECT "django_session"."session_key", "django_session"."session_data", "django_session"."expire_date" FROM "django_session" WHERE ("django_session"."session_key" = '0ayyigap4sphq5lvtlfy64una45o4id6'  AND "django_session"."expire_date" > '2013-05-27 10:38:44.800431+00:00' ); args=('0ayyigap4sphq5lvtlfy64una45o4id6', u'2013-05-27 10:38:44.800431+00:00')
DEBUG (0.002) SELECT "my_auth_profile"."id", "my_auth_profile"."password", "my_auth_profile"."last_login", "my_auth_profile"."is_superuser", "my_auth_profile"."username", "my_auth_profile"."first_name", "my_auth_profile"."last_name", "my_auth_profile"."email", "my_auth_profile"."is_staff", "my_auth_profile"."is_active", "my_auth_profile"."date_joined", "my_auth_profile"."cost_hour", "my_auth_profile"."last_activity" FROM "my_auth_profile" WHERE "my_auth_profile"."id" = 3 ; args=(3,)
DEBUG (0.001) SELECT "auth_group"."name" FROM "auth_group" INNER JOIN "my_auth_profile_groups" ON ("auth_group"."id" = "my_auth_profile_groups"."group_id") WHERE "my_auth_profile_groups"."profile_id" = 3 ; args=(3,)

这是最后一个查询来自 BTW 的地方:

my_auth.context_processors.py

def extended_auth(request):
    '''
    @allowed_user (bool) the user is either a Core team member or an admin.
    @user_groups (string) the groups the user belongs to.
    '''
    is_allowed = False
    user_groups = None
    if not request.user.is_anonymous():
        tmp = request.user.groups.values('name')
        user_groups = [g['name'] for g in tmp]
        if 'Core' in user_groups or 'Administrator' in user_groups:
            is_allowed = True
    return {'allowed_user': is_allowed, 'user_groups': user_groups, }
4

1 回答 1

1

current transaction is aborted, commands ignored until end of transaction block告诉您之前的语句导致了导致自动事务回滚的错误。

您需要检查您的错误日志以了解上一个失败的语句。如果在 Django 的日志中找不到,请查看 PostgreSQL 的日志。

于 2013-05-27T12:26:21.850 回答