0
votes

i have a code , i have login form whenever the user fill login form than form will render to home page. but i want to print username in the home page who is logged in.

views.py :

this is login function , when user fill login form than it authenticate the values and render to the home page.

def userLogin(request,template=None,context=None):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/home/')
    if request.method == 'POST':
        form = userLoginform(request.POST)
        if form.is_valid():
#           authenticated_user = form.save()
#           login(request, authenticated_user)
            return HttpResponseRedirect("/home/")
        else:
            template=u"registration/login.html"
            context= {"form": form}
            return render(request, template,context)
    else:
        form=userLoginform()
        template=u"registration/login.html"
        context= {"form": form}
    return render(request, template,context)

whenever i use this code , the user is logged in properly but the session value is not working , when i used {{ user.username }} in home page then it will not display session values.

1
It would be helpful to post the view code that is related to the homepage...however, try {{ request.user.username }} instead, unless you are passing the request.user as user in your context dictionary.Joseph Paetz
@JosephPaetz to be able to use request.user.username in the template he will need to add 'django.core.context_processors.request' to his settingsuser1593705

1 Answers

1
votes

Like suggested by Joseph you need to set the request.user to the context like :

    context= {"form": form, "user": request.user}
    return render(request, template,context)

thus in your template you will beable to access to {{ user.username }}

you can have a look at the complete doc about how to use the auth system of django

hope this could help