0

How would I use the context value to check if the currently logged in user has the same team and it's not null and the is_edit field is true.

{% if request.user.is_edit and team==request.user.team%}

produces

Could not parse the remainder: '==request.user.team' from 'team==request.user.team' 

edit.html

{% extends 'base.html' %} {% block title %}Add{% endblock %} {% block content %} <h1 class="cardHeader">Edit Page</h1> <div class="card"> <div class="container"> <h1>Edit team member</h1> <p>Edit contact info,location and role</p> </div> <form method = "POST" action="" enctype="multipart/form-data"> {% csrf_token %} {{ editProfileForm.as_p }} <input type="submit" name="editProfileForm" value="Save"> <!-- Check if the user who can delete the team member is the same team and had admin role? --> {% if request.user.is_edit and team==request.user.team%} <input type="submit" name="deleteProfileForm" value="Delete"> {% endif %} </form> </div> {% endblock %} 

views.py

def edit(request,user_id): context={} try: user = Profile.objects.get(id=user_id) print(user,user_id) except Exception as e: print(str(e)) return redirect('home') context['team']=user.team #Currently logged in user has to have is_edit? and be the same team #if request.user.team != user.team and request.user.is_edit: #return redirect('home') if request.method=="POST": if "editProfileForm" in request.POST: print('Editing') edit_profile_form = UserProfileForm(request.POST or None,instance=user) if edit_profile_form.is_valid(): edit_profile_form.save() print(edit_profile_form) return redirect('home') else: context['editProfileForm'] = edit_profile_form return render(request,'edit.html',context) if "deleteProfileForm" in request.POST: print('Deleting') try: user.delete() #messages.success(request, "The user is deleted") except Exception as e: #messages.error(request, "User does not exist") print(str(e)) pass return redirect('home') else: edit_profile_form = UserProfileForm(request.POST or None,instance=user) context['editProfileForm'] = edit_profile_form return render(request,'edit.html',context) 
5
  • 2
    Add spaces around the operator - team == request.user.team Commented Jul 7, 2022 at 5:00
  • Still makes it empy. But the error is gone. Commented Jul 7, 2022 at 5:29
  • I see you have user.team where user = Profile.objects.get(id=user_id), are you sure request.user is an instance of Profile? Commented Jul 7, 2022 at 5:37
  • print(user.team, request.user.team)->Team object (6ab326b7-6fa0-48ea-8a85-5f36c32635cc) does produce the right value. Commented Jul 7, 2022 at 6:57
  • request.user.is_edit didn't work until I passed it from the view as a context value. Commented Jul 7, 2022 at 7:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.