python - Django why can't I authenticate a user? -


i'm trying create user authentification form using django +1.8 encountered problem, when press form submit button, nothing happens, page reloading instead of loging in, why?

here code:

views.py

from django.shortcuts import render django.contrib.auth import authenticate django.contrib.auth import login auth_login django.contrib.auth import logout auth_logout  # create views here. def user(request):     context = {}     return render(request, 'user.html', context)  def login(request):     context = {}     if request.method == "post":         username = request.post['username']         password = request.post['password']         user = authenticate(username=username, password=password)         if user not none:             if user not none:                 auth_login(request, user)             else:                 context['error'] = 'non active user'         else:             context['error'] = 'mauvais nom d\'utilisateur ou mot de passe'     else:         context['error'] = ''     return render(request, 'user.html', context)  def logout(request):     context = {}     if request.method == "post":         auth_logout(request)     else:         context['error'] = 'une erreur s\'est produite.'     return render(request, 'user.html', context) 

urls.py

    django.conf.urls import url     utilisateur import views      urlpatterns = [         url(r'^$', views.user, name="user"),         url(r'^login$', views.login, name='login'),         url(r'^logout$', views.logout, name='logout'),     ] 

user.html

{% block content %} {% if user.authenticated %} <p>{{ user.username }}</p> <a href="logout?">logout</a> {% else %} <form method="post" action="login">     {% csrf_token %}     <p>username:<input type="text" name="username"></p>     <p>password:<input type="password" name="password"></p>     <input value="se connecter" type="submit"> </form> {% endif %} {% endblock %} 

i thought middlewares useful in order solve issue.

middleware_classes = [     'django.middleware.security.securitymiddleware',     'django.contrib.sessions.middleware.sessionmiddleware',     'django.middleware.common.commonmiddleware',     'django.middleware.csrf.csrfviewmiddleware',     'django.contrib.auth.middleware.authenticationmiddleware',     'django.contrib.auth.middleware.sessionauthenticationmiddleware',     'django.contrib.messages.middleware.messagemiddleware',     'django.middleware.clickjacking.xframeoptionsmiddleware', ]  'context_processors': [      'django.template.context_processors.debug',      'django.template.context_processors.request',      'django.contrib.auth.context_processors.auth',      'django.contrib.messages.context_processors.messages', ], 

i think problem comes context_instance=requestcontext(request), how can solve ?

actually authentication working properly. need make corrections like, user.authemticated user.is_authenticated , change logout function , url on template

user.html:

{% block content %} {% if user.is_authenticated %} <p>{{ user.username }}</p>  <a href="/logout">logout</a> {% else %} <form method="post" action="login">     {% csrf_token %}     <p>username:<input type="text" name="username"></p>     <p>password:<input type="password" name="password"></p>     <input value="se connecter" type="submit"> </form> {% endif %} {% endblock %} 

views.py

from django.shortcuts import render django.contrib.auth import authenticate django.contrib.auth import login auth_login django.contrib.auth import logout auth_logout  # create views here. def user(request):     context = {}     return render(request, 'user.html', context)  def login(request):     context = {}     if request.method == "post":         username = request.post['username']         password = request.post['password']         user = authenticate(username=username, password=password)         if user not none:             if user.is_active:                 auth_login(request, user)             else:                 context['error'] = 'non active user'         else:             context['error'] = 'mauvais nom d\'utilisateur ou mot de passe'     else:         context['error'] = ''     return render(request, 'user.html', context)  def logout(request):     context = {}     auth_logout(request)     return render(request, 'user.html', context) 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -