1

I have a class called Schedule with a field (is this correct?) I've set using admins = models.ManyToManyField(User). This field contains a list of users I can select multiples of.

In the view for the schedule, I show a bunch of information. I would like to show a couple additional things based on if the currently logged in user is included in the admins of that schedule being viewed.

1
  • BTW.. When I say View, I mean showing it in the template. Not in the views.py Commented Mar 26, 2013 at 2:53

1 Answer 1

3

According to Django philosophy, you should have your business logic within views and presentation logic in the template. So the computation if the logged user is among the admins should be done in a view, and if the user is, then what is displayed should be determined in the template. You can accomplish that by:

# views.py def schedule(request, id): schedule = get_object_or_404(Schedule, pk=id) if request.user.is_authenticated(): is_admin = schedule.admins.filter(pk=schedule.pk).exists() else: is_admin = False data = { 'schedule': schedule, 'is_admin': is_admin, } return render_to_response('template.html', data) # template.html {% if is_admin %} <p>You are an admin of the schedule!</p> {% else %} <p>Sorry. You are not an admin of the schedule</p> {% endif %} 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.