Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top