对象没有属性会话

时间:2013-03-27 19:28:52

标签: python django

我正在为库存创建一个签入/结帐应用程序。用户不需要密码或电子邮件,只需输入数字或扫描带有条形码的卡。因此,我使用自己的名为Members而不是使用auth / users模型。 Eveything工作正常,我可以登录/注销,但是当我尝试结帐时问题就开始了。然后我收到错误Member object has no attribute 'session'

如何解决此错误?

错误

[27/Mar/2013 12:16:30] ERROR [django.request:215] Internal Server Error: /checkout/
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/username/Google Drive/Python/GearTracker/GearTracker/decorators.py", line 15, in wrapper
    return function(request, *args, **kw)
  File "/Users/username/Google Drive/Python/GearTracker/inventory/views.py", line 28, in checkout
    check_status = check_in_out_item(request.session.get('user'),checked_out_item)
  File "/Users/username/Google Drive/Python/GearTracker/GearTracker/decorators.py", line 12, in wrapper
    if not (request.session.get('is_authed') == True):
AttributeError: 'Member' object has no attribute 'session'

decorators.py

def is_user_authed(function):
    def wrapper(request, *args, **kw):
        logger.info(request)
        if not (request.session.get('is_authed') == True):
            return HttpResponseRedirect('/login/')
        else:
            return function(request, *args, **kw)
    return wrapper

inventory / views.py代码段

@is_user_authed
def checkout(request):
    form = CheckoutForm(request.POST or None)
    logger.info(request)
    if request.method == 'POST':
        if (request.POST.get('serial')):
            try:
                checked_out_item = Inventory.objects.get(serial=request.POST.get('serial'))
                check_status = check_in_out_item(request.session.get('user'),checked_out_item)
            except Inventory.DoesNotExist:
                pass
        elif (request.POST.get('barcode')):
            try:
                checked_out_item = Inventory.objects.get(barcode=request.POST.get('barcode'))
                check_status = check_in_out_item(request.session.get('user'),checked_out_item)
            except Inventory.DoesNotExist:
                pass

    checked_out_items = Checkout.objects.all().filter(user=request.session.get('user'),checked_in__isnull=True)

    return render(request, 'inventory/index.html', {'items': checked_out_items, 'form': form})

广告资源模型

class Group(models.Model):
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

class Member(models.Model):
    name = models.CharField(max_length=200)
    p_number = models.BigIntegerField()
    barcode = models.CharField(max_length=50,blank=True)

    def __unicode__(self):
        return self.name

class Inventory(models.Model):
    name = models.CharField(max_length=200)
    serial = models.CharField(max_length=200)
    barcode = models.CharField(max_length=200)
    active = models.BooleanField(verbose_name="Active (can be checked out if not out for repair)",blank=True,default=True)
    repair = models.BooleanField(verbose_name="Out for repair?",blank=True)
    group = models.ForeignKey(Group)

    def __unicode__(self):
        return self.name

class Checkout(models.Model):
    user = models.ForeignKey(Member)
    item = models.ForeignKey(Inventory)
    checked_out = models.DateTimeField()
    checked_in = models.DateTimeField(null=True, blank=True)

    def __unicode__(self):
        return self.item.name

中间件

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

2 个答案:

答案 0 :(得分:1)

正如评论中所述,问题是check_in_out_item期待Request,因为你已经装饰了它,但是你传递的是userMember }}。您需要修改check_in_out_item以接受Request而不是Member或删除装饰器。

答案 1 :(得分:0)

要解决此错误,请先查看您使用的是哪个TensorFlow版本,如果看到的是TensorFlow版本1.14,则将解决此问题。