Reputation: 17
I would like to display information obtained from an SQLite Database, but I would only want the data to be displayed that is related to the user.
Since the email of each user is unique, I want it to see if it matches the email of the current signed user. Below is the code for the corresponding view above
Update: I have just attempted to do a filter
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)
Working Updated view (With reference to the answer given):
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})
Upvotes: 0
Views: 206
Reputation: 9384
That will be something like
if user.get('email', None):
SaveClaimForm.objects.filter(email=user.get('email', None))
else:
return HttpResponse('Unauthorized', status=401)
Instead of
SaveClaimForm.objects.all()
Upvotes: 2