在庫用のチェックイン/チェックアウト アプリケーションを作成しています。ユーザーはパスワードや電子メールを必要とせず、番号を入力するか、バーコード付きのカードをスキャンするだけで済みます。したがって、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
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',
)