Problem with seassion

18 views
Skip to first unread message

Dariusz Mysior

unread,
Nov 13, 2015, 3:59:04 AM11/13/15
to Django users
In the application accounts have everything that regards registration and login and beyond this application in the project folder I have index.html to the home page where I have links to login and registration forms, I would like to index.html in some way to provide request.user.username that instead of these two URLs I showed the message that I'm logged in.

How can I request.user.username application file accounts views.py move to file views.py main project? : /

I paste here all what I already have

index.html from the root folder of the project

{% load staticfiles %}
 
<link rel="stylesheet" type="text/css" href="{% static 'accounts/css/style.css' %}" />
 
{% block content %}
{% if user.is_authenticated %}
   
<p>Jesteś zalogowany {{ username }}</p>
{% else %}
   
<h2>Strona główna</h2>
<a href='/accounts/login_view'>logowanie</a>
<a href='/accounts/register_user'>rejestracja</a>
{% endif %}
 
 
{% endblock %}

views.py from the root folder of the project

from django.shortcuts import render_to_response
 
def index(request):
   
return render_to_response('index.html', {'username': request.user.username})

accounts/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, logout
 
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/my_view')
           
else:
               
# Return a 'disabled account' error message
               
...
       
else:
           
# Return an 'invalid login' error message.
           
...
 
   
form = AuthenticationForm()
    args
= {}
    args
.update(csrf(request))
    args
['form']= AuthenticationForm()
   
return render_to_response('accounts/login.html', args)
def my_view(request):
 
   
return render_to_response('accounts/my_view.html', {'username': request.user.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')
 
def logout(request):
   
pass

accounts/my_view

{% load staticfiles %}
 
<link rel="stylesheet" type="text/css" href="{% static 'accounts/css/style.css' %}" />
 
{% block content %}
<h2>My profile</h2>
<p>Witaj {{ username }}</p>
{% endblock %}


Dariusz Mysior

unread,
Nov 13, 2015, 4:24:04 AM11/13/15
to Django users
Ok I have it! :)

views.py

def index(request):
   return render_to_response('index.html', {'username': request.user.username,
                                            'user':request.user})

index.html

{% load staticfiles %}
 
<link rel="stylesheet" type="text/css" href="{% static 'accounts/css/style.css' %}" />
 
{% block content %}
{% if user.is_authenticated %}

   
<p>Jesteś zalogowany {{ user.username }}</p>

{% else %}
   
<h2>Strona główna</h2>
<a href='/accounts/login_view'>logowanie</a>
<a href='/accounts/register_user'>rejestracja</a>
 
{% endif %}
 
 
{% endblock %}
...
Reply all
Reply to author
Forward
0 new messages