How to get logged username in template

399 views
Skip to first unread message

Dariusz Mysior

unread,
Nov 12, 2015, 2:46:06 AM11/12/15
to Django users

I am using Django 1.8 with Python 3.4 I had no idea why my template doesn't show my username on template profile.html :/


profile.py


{% load staticfiles %}

<link rel="stylesheet" type="text/css" href="{% static 'accounts/css/style.css' %}" />

{% block content %}
<h2>My profile</h2>
<p>{{ request.user.username }}</
p>
{% endblock %}

views.py


from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.shortcuts import render_to_response
from django.http import  HttpResponseRedirect
from django.core.context_processors import csrf
from django.contrib.auth import authenticate, login


def login_view(request):

   
if request.method == 'POST':
        username
= request.POST['username']
        password
= request.POST['password']
        user
= authenticate(username=username, password=password)
       
if user is not None:
           
if user.is_active:
                login
(request, user)
               
return HttpResponseRedirect('/accounts/profile')
           
else:
               
# Return a 'disabled account' error message
               
...
               
pass
       
else:
           
# Return an 'invalid login' error message.
           
pass
    form
= AuthenticationForm()
    args
= {}
    args
.update(csrf(request))
    args
['form']= AuthenticationForm()
   
return render_to_response('accounts/login.html', args)

def my_view(request):
    username
= request.POST['username']
    password
= request.POST['password']
    user
= authenticate(username=username, password=password)
   
if user is not None:
       
print(request.user)
       
if user.is_active:
            login
(request, user)
           
return HttpResponseRedirect('/accounts/profile')
       
else:
           
# Return a 'disabled account' error message
           
...
   
else:
       
# Return an 'invalid login' error message.
       
...
def profile(request):
    username
= request.user.username
   
return render_to_response('accounts/profile.html', username)

def register_user(request):
   
if request.method == 'POST':
        form
= UserCreationForm(request.POST)
       
if form.is_valid():
            form
.save()
           
return HttpResponseRedirect('/accounts/register_success')
    args
= {}
    args
.update(csrf(request))
    args
['form']= UserCreationForm()
   
return render_to_response('accounts/register_user.html', args)

def register_success(request):
   
return render_to_response('accounts/register_success.html')


Andreas Kuhne

unread,
Nov 12, 2015, 3:58:25 AM11/12/15
to django...@googlegroups.com
Hi,

First of all, you are using request.user.username and not username in your template. If you want the username to be accessed via the "username" variable. You should write this in your view:
def profile(request):
   
return render_to_response('accounts/profile.html', {'username': request.user.username})
And then in your template use the following code:
<p>{{ username }}</p>

However if you want to use the request object you should use the RequestContext context when rendering the page. See https://docs.djangoproject.com/en/1.8/ref/templates/api/#subclassing-context-requestcontext

Also because you are using django 1.8, I would recommend that you start using class based views instead of view functions. But that's just my preference :-)

Regards,

Andréas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/88ec2289-59b6-41a3-a1d7-c41be3fc0b4b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dan Tagg

unread,
Nov 12, 2015, 4:25:26 AM11/12/15
to django...@googlegroups.com
I would recommend using allauth http://django-allauth.readthedocs.org/en/latest/templates.html. It has template tags for adding all that info, creates pages for logging in, picks up your base template etc etc.

Dan


For more options, visit https://groups.google.com/d/optout.



--
Wildman and Herring Limited, Registered Office: 52 Great Eastern Street, London, EC2A 3EP, Company no: 05766374

Dariusz Mysior

unread,
Nov 12, 2015, 6:46:56 AM11/12/15
to Django users
Thanks for both advice. I am not advanced programmer sow I use the simplest solution on this moment this first one Andréas Kühne. Thanks it works!
Reply all
Reply to author
Forward
0 new messages