Problem with login and register form

95 views
Skip to first unread message

Dariusz Mysior

unread,
Nov 24, 2015, 3:34:30 PM11/24/15
to Django users
Hi I had a problem with display a form of login and register, please look on it.

my template access_ownsite.html

{% load staticfiles %}

<!DOCTYPE html>
<html lang="en">
<head>
   
<title>{% block title %}{% endblock %}</title>
   
<link rel="stylesheet" type="text/css" href="{% static 'ownsite/css/style.css' %}" />


</head>
<body>
{% block content %}
<div id="header">
    {% if user.is_authenticated %}
       
<p>Jesteś zalogowany <a href='ownsite/my_view'>{{ user.username }}</a></p>
       
<p><a href='/accounts/logout_view'>wyloguj</a></p>
    {% else %}
       
<h2>Login</h2>
       
<form action = '/accounts/login_view/' method = 'post'>{% csrf_token %}
        {{form.as_p}}
       
<input type='submit' value='Log' />
       
</form>
    {% endif %}
</div>
<div id="content">
   
<div id="left_content">
        {% block logout_msg %}
           
<p> {{ info }} </p>
           
<p>Nikt nie jest zalogowany w tym momencie.</p>
        {% endblock %}
   
</div>
   
<div id="right_content">
        {% if user.is_authenticated == False %}
           
<h2>Register</h2>
           
<form action = '/accounts/register_user/' method = 'post'>{% csrf_token %}
                {{form.as_p}}
               
<input type='submit' value='Register' />
           
</form>
        {% endif %}
   
</div>
</div>
<div id="footer">
<p>copyright &copy; Dariusz Mysior</p>
</div>
{% endblock %}
</body>
</html>


my view.py

from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.shortcuts import render_to_response, render
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('ownsite/access_ownsite.html', args)#accounts/login.html
def my_view(request):

   
return render_to_response('accounts/my_view.html', {'username': request.user.username,'user': request.user })

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_view(request):
    logout
(request)
   
return render_to_response('accounts/logout_view.html', {'info': 'Właśnie się wylogowałeś.',})







Caique Reinhold

unread,
Nov 24, 2015, 5:52:15 PM11/24/15
to Django users
You are using the same form for both register and login. Set different names for the forms in the template context.
...

Dariusz Mysior

unread,
Nov 25, 2015, 1:05:26 PM11/25/15
to Django users
Hmm I forget about it but I change names on form_l and form_r and effect is the same, I had also info


C:\Python34\lib\site-packages\django\template\defaulttags.py:66: UserWarning: A {% csrf_token %} was used in a template, but the context did not provide the value.  This is usually cause
d by not using RequestContext.
  "A {% csrf_token %} was used in a template, but the context "

...

knbk

unread,
Nov 25, 2015, 1:08:37 PM11/25/15
to Django users
It's a good idea to use render() instead of render_to_response(), see https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/#render. That will fix the issue with the CSRF token. render() takes the request as the first argument, and uses a RequestContext. This is needed to run the context processors, which add the csrf_token to the template. 

Dariusz Mysior

unread,
Nov 25, 2015, 1:20:27 PM11/25/15
to Django users
It's not fix a problem.


W dniu wtorek, 24 listopada 2015 21:34:30 UTC+1 użytkownik Dariusz Mysior napisał:
...

Dariusz Mysior

unread,
Nov 25, 2015, 1:24:29 PM11/25/15
to Django users
Before template access_ownsite.html I had separated on few templates like login, register etc .html and it work's but now when I split it, a form is not forward to access_ownsite.html


W dniu wtorek, 24 listopada 2015 21:34:30 UTC+1 użytkownik Dariusz Mysior napisał:
...

Caique Reinhold

unread,
Nov 27, 2015, 5:20:09 PM11/27/15
to Django users
You should send both forms to the context, as in:


args['form_login'] = AuthenticationForm()
args['form_register'] = UserCreationForm()

And change the template to the appropriate form names. Also, change {% if user.is_authenticated == False %} to {% if not user.is_authenticated %}.
Reply all
Reply to author
Forward
0 new messages