我正在为库存创建一个签入/结帐应用程序。用户不需要密码或电子邮件,只需输入数字或使用条形码扫描卡。因此,我没有使用 auth / users 模型,而是使用我自己的名为Members
. 一切工作正常,我可以登录/注销,但是当我尝试结帐时问题就开始了。然后我得到一个错误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'
装饰器.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
库存/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',
)