1

I want to create a simple Django authentication (register/login/logout) but I have problems. if complete login successfully then app go to next page with name mypage because I have define in my settings.py that line : LOGIN_REDIRECT_URL = '/mypage'.

but in the new page user is not authenticated user(my user stay authenticated only in the "/login"and"/register"page but not in home and mypage )

any idea why I have wrong ?

here the code :

urls.py

url(r'^$', views.home), url(r'^mypage/$', views.mypage), url(r'^login/$', views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}), url(r'^logout/$', views.logout, {'next_page': '/login/'}), url(r'^register/$', views.register), url(r'^register/success/$', views.register_success) 

views.py

def home(request): return render_to_response('home.html') def mypage(request): return render_to_response('home2.html') 

settings.py

LOGIN_REDIRECT_URL = '/mypage' 

html 1 :

 {% if user.is_authenticated %} <li> <a href="/logout">LogOut</a> </li> {% else %} <li> <a href="/login">Login</a> </li> {% endif %} 

html 2 :

{% if user.is_authenticated %} <a href="#" class="lg">Start</a> {% else %} <p>Please Login to start</p> {% endif %} 
5
  • Include views.py Commented Sep 10, 2017 at 1:30
  • @Astik Anand where Include views.py ? Commented Sep 10, 2017 at 1:32
  • I mean include your views also here in question. Commented Sep 10, 2017 at 1:43
  • @Astik Anand that is all my view with register Commented Sep 10, 2017 at 1:48
  • @ Astik Anand any idea ? Commented Sep 10, 2017 at 1:48

2 Answers 2

1

when you use render_to_response you need add user to context manual, or you can use render with request, in this case user will be added to your template context

from django.shortcuts import render, render_to_response def home(request): return render(request, 'home.html') def mypage(request): return render_to_response('home2.html', {'user': request.user}) 
Sign up to request clarification or add additional context in comments.

2 Comments

I think so working now thank you that need to do for all pages where I want to have user authenticated like this return render_to_response('home2.html', {'user': request.user}) ?
if you want to use render_to_response, yes.
1

In urls.py

from django.contrib.auth import views as auth_views urlpatterns = [ url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'), url(r'^logout/$', auth_views.logout, name='logout'), . . .. ] 

In login.html

{% extends 'base.html' %} {% block title %}Login{% endblock %} {% block content %} <h2>Login</h2> <form method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit">Login</button> </form> {% endblock %} 

5 Comments

is the some again user is not authenticated in mypage..that as auth_views must be use for all regex
Not required to do that.
Try to print something after login to make sure it works, something like <h2>{{user.username}}</h2> and let me know.
Have you tried to print username on your home2.html, what it says??
I can see user olny in login and register page

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.