Whenever I run this,
Exception Value:
name ‘current_user’ is not defined;
error is raised.
I am not getting where i am doing the mistake as I m new in django programming. Please help me fetch the data
JavaScript
x
32
32
1
# To add a new product in the database
2
def AddNewProduct(request):
3
if request.method == "POST":
4
current_user = request.user
5
product_title =request.POST['product_title']
6
uid = request.POST['uid']
7
specification =request.POST['specification']
8
sale_price = request.POST['sale_price']
9
discount = request.POST['discount']
10
img1 = request.FILES['img1']
11
img2 = request.FILES['img2']
12
promote_method = request.POST['promote_method']
13
terms_conditions = request.POST['terms_conditions']
14
newproduct = AffProduct(user_id=current_user.id, product_title=product_title, uid=uid, specification=specification, sale_price=sale_price,
15
discount=discount, img1=request.FILES.get('img1'), img2=request.FILES.get('img2'),
16
promote_method=promote_method, terms_conditions=terms_conditions)
17
newproduct.save()
18
# Status message
19
messages.success(request, 'Product added successfully')
20
21
return render(request, 'blink_network.html')
22
else:
23
return render(request, 'blink_network.html')
24
25
#Here i m trying to fetch my data.
26
def showproduct(request):
27
if request.user.is_authenticated:
28
result = AffProduct.objects.filter(user_id=current_user.id)
29
else:
30
result = AffProduct.objects.all()
31
return render(request, 'blink_viewproduct.html', {'result': result})
32
Advertisement
Answer
It looks like you will be getting that problem from showproduct(request)
because you don’t define current_user
in that method before calling it.
to call this
result = AffProduct.objects.filter(user_id=current_user.id)
you need to define current_user = request.user
beforehand