Home > Software engineering >  Need a condition where User can't complain about himself in Django
Need a condition where User can't complain about himself in Django

Time:04-19

This is a Complain form where a logged in user can submit. I want a condition in views.py file, where a logged in user can't submit a complain form against himself. 

-------- SKIP Please---------

CodePudding user response:

You can add a condition to your view that checks the request user against the user being complained about. If they are the same, you can redirect or render a template with an error message.

For example:

def complain(request, user_id):
    if request.user.id == user_id:
        # redirect or render error template
    else:
        # continue with view logic

CodePudding user response:

Here try this:

if request.method=='POST':  
        
        email = request.POST['email']
        complain = request.POST['complain']
        against = request.POST['against']
        image = request.FILES.get('image')
        user = User.objects.filter(username = against)

        if user.first() is not None:
                if request.user == user.first():
                    # raise error
                    pass
                complain = Complain(email = email,  complain=complain, against = against, image=image)
                complain.save()
                messages.success(request, 'Complain Submit Successful')
                return redirect('complain')
        else:
                messages.error(request, 'You are complaining against Non-User (-,-)')
                return redirect('complain')
    
else:
    return render(request,'complain.html')
  • Related