我想显示从 SQLite 数据库获取的信息,但我只想显示与用户相关的数据。
由于每个用户的电子邮件都是唯一的,我希望它查看是否与当前签名用户的电子邮件匹配。下面是上面对应视图的代码
更新:我刚刚尝试做一个过滤器
claims = SaveClaimForm.objects.filter(email=user.email)
def newclaim(request):
context = initialize_context(request)
user = context['user']
if request.method == 'POST':
name = request.POST['name_field']
email = request.POST['email_field']
claim = request.POST['claim_field']
claimtype = request.POST.get('claimtype')
description = request.POST['description_field']
receipt = request.POST.get('receipt_field')
cheque = request.POST.get('cheque_field')
ins = SaveClaimForm(name=name, email=email, claim=claim, claimtype=claimtype, description=description, receipt=receipt, cheque=cheque)
ins.save()
print("The Data has been written")
return render(request, 'Login/newclaim.html/', context)
<块引用>
工作更新视图(参考给出的答案):
def viewclaims(request):
context = initialize_context(request)
user = context['user']
if user.get('email', None):
claims = SaveClaimForm.objects.filter(email=user.get('email', None))
return render(request, 'Login/existingclaims.html', {'claims':claims, 'user':user})
答案 0 :(得分:2)
应该是这样的
if user.get('email', None):
SaveClaimForm.objects.filter(email=user.get('email', None))
else:
return HttpResponse('Unauthorized', status=401)
代替
SaveClaimForm.objects.all()