django - Show information based on users -


i have class called schedule field (is correct?) i've set using admins = models.manytomanyfield(user). field contains list of users can select multiples of.

in view schedule, show bunch of information. show couple additional things based on if logged in user included in admins of schedule being viewed.

according django philosophy, should have business logic within views , presentation logic in template. computation if logged user among admins should done in view, , if user is, displayed should determined in template. can accomplish 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 admin of schedule!</p> {% else %}     <p>sorry. not admin of schedule</p> {% endif %} 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -