2

In my django project, I had to create some static pages like help page, about us page, faq page etc, and I needed that the user name showed up somewhere on these pages, but since they are called by generic views, I don't know how to send variables to templates when it's about generic views.

Thanks in advance.

smarber
  • 4,829
  • 7
  • 37
  • 78
  • I think your question was already answered here: [http://stackoverflow.com/questions/7470539/passing-request-user-to-template-in-django][1] [1]: http://stackoverflow.com/questions/7470539/passing-request-user-to-template-in-django – AgileDeveloper Jun 29 '13 at 23:49

2 Answers2

3

In this case there is no need to pass the username to the template if you use djangos auth system. You can access request.user in your template. Have a look here How to access the user profile in a Django template?. Anyway, if you want to pass in additional variables to a generic view, look here How to pass parameters to django generic views.

Community
  • 1
  • 1
Jingo
  • 3,200
  • 22
  • 29
2

See the post that @Jingo said and, if you want to add some variables to generic views, just overwrite the ´get_context_data´ function.

class ExampleView(TemplateView): # Or another generic view
    template_name = "example.html"

    def get_context_data(self, **kwargs):
        context = super(ExampleView, self).get_context_data(**kwargs)
        #If you dont call 'super', you wont have the context processor varibles
        #  like 'user'
        context['var_name'] = "var content" # you can add template variables!
        return context # dont forget to return it!
Leandro
  • 2,217
  • 15
  • 18