0

I have seen similar questions and answers but none that address my problem.

I want my view to perform a User Group check and then pass that via variable to the template. The template will then use that to appear differently to different user groups.

My views.py:

def cans(request):
    is_canner = request.user.groups.filter(name='canner') #check if user group = canner
    can_list = Can.objects.order_by('name')
    context = {'can_list': can_list}
    return render(request, 'cans/cans.html', context) #need to return is_canner variable here

And in my template I would use the variable like so:

{% if is_canner %} canner stuff goes here {% endif %}

I'm unsure how to pass this variable, I thought it used context to send it like so:

return render(request, 'cans/cans.html', context({"is_canner": is_canner}))

But this gives me errors - context is not callable.

Dawson
  • 457
  • 2
  • 9
  • 21

1 Answers1

6

context is not a function, its an argument to the render function, e.g.

context = {"is_canner": is_canner}
return render(request, 'cans/cans.html', context)

docs: https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render

more background info: Django - what is the difference between render(), render_to_response() and direct_to_template()?

Community
  • 1
  • 1
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
  • This gives me an error when loading the template: render_to_string() got an unexpected keyword argument 'context' – Dawson Oct 06 '14 at 07:50