[Answer]-Display user data in template

1πŸ‘

βœ…

user is automatically provided to the template via the context processor. But these only run if you are using a RequestContext when rendering your template: either by specifically passing it in (eg with the context_instance parameter to render_to_response), or by using the newer render shortcut.

So in all your other views, you need to be sure you are doing:

return render(request, 'your_template.html', params)

0πŸ‘

Looks like the user object is not available inside your template. If the user object is not available in the template, nothing will be displayed when the template evaluates {{ user.username }}

From the docs –

If you use a variable that doesn’t exist, the template system will
insert the value of the TEMPLATE_STRING_IF_INVALID setting, which is
set to ” (the empty string) by default.

You can pass the user object to the template by modifying the appropriate view which returns the welcome template, using either render() which forces the use of a RequestContext implicitly or render_to_response() where you have to explicitly pass a RequestContext instance

πŸ‘€dannymilsom

0πŸ‘

in your views.py you should use RequestContext

from django.template import RequestContext
from django.shortcuts import render_to_response

def login(request):
args={}
...
return render_to_response('login.html', args, context_instance=RequestContext(request))
πŸ‘€Marius Darila

Leave a comment