Skip to content
Advertisement

How can i fetch my data from database to django template?

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

# To add a new product in the database
def AddNewProduct(request):
    if request.method == "POST":
        current_user = request.user
        product_title =request.POST['product_title']
        uid = request.POST['uid']
        specification =request.POST['specification']
        sale_price = request.POST['sale_price']
        discount = request.POST['discount']
        img1 = request.FILES['img1']
        img2 = request.FILES['img2']
        promote_method = request.POST['promote_method']
        terms_conditions = request.POST['terms_conditions']
        newproduct = AffProduct(user_id=current_user.id, product_title=product_title, uid=uid, specification=specification, sale_price=sale_price,
                                discount=discount, img1=request.FILES.get('img1'), img2=request.FILES.get('img2'),
                                promote_method=promote_method, terms_conditions=terms_conditions)
        newproduct.save()
        # Status message
        messages.success(request, 'Product added successfully')

        return render(request, 'blink_network.html')
    else:
        return render(request, 'blink_network.html')

#Here i m trying to fetch my data.
def showproduct(request):
    if request.user.is_authenticated:
        result = AffProduct.objects.filter(user_id=current_user.id)
    else:
        result = AffProduct.objects.all()
    return render(request, 'blink_viewproduct.html', {'result': result})

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

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement