我正在为库存创建一个签入/结帐应用程序。用户不需要密码或电子邮件,只需要输入一个数字或用他们的条形码扫描一张卡。所以我没有使用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'
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
库存/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})
<<p> 库存模型/strong> 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',
)
正如评论中所述,问题是check_in_out_item
期待Request
,因为你已经装饰了它,但你正在传递它user
,这是Member
。您需要修改check_in_out_item
以接受Request
而不是Member
,或者删除装饰符