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

2015-11-12 8:46 GMT+01:00 Dariusz Mysior <mysior.da...@gmail.com>:

> 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')
>
>
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@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
> <https://groups.google.com/d/msgid/django-users/88ec2289-59b6-41a3-a1d7-c41be3fc0b4b%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CALXYUbkQvGe%2B1%3DDAc6A%3DMv4S_wYSYjF_Nzcnt3P9Q%3DU%3DJ%2BtZ6A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to