Thursday, November 26, 2020

Unable to Login

Hello , am new to Django and i created a login page ,but unable to get login , please help me to solve this , thank you in advance 





views.py : 
===========

from django.views import *
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login

# Create your views here.
def signup(request):
    if request.method == "POST":
        if request.POST['password1'] == request.POST['password2']:
            try:
                User.objects.get(username=request.POST['username'])
                return render(request,'accounts/signup.html',{'error':"Username already exist"})
            except User.DoesNotExist:
                user=User.objects.create_user(request.POST['username'],request.POST['password1'])
                login(request,user)
                return render(request,'accounts/signup.html',{'error':"user account created  Successfully "})
        else:
            return render(request,'accounts/signup.html',{'error':"password didnt match"})
    else:
        return render(request,'accounts/signup.html')

def loginview(request):
    if request.method=="POST":
         username=request.POST['username']
         password=request.POST['password']
         user=authenticate(username=username,password=password)
         if user is not None:
             login(request,user)
             return render(request,'accounts/login.html',{"error":"Login successful!!"})
         else:
            return render(request,'accounts/login.html',{"error":"username and password didnt match"})
    else:
         return render(request,'accounts/login.html')


Note : when i enter login credentials , i unable to get message " Login successful" . please solve me on this  


urls.py :
=====

from django.contrib import admin
from django.urls import path
import accounts.views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('signup/', accounts.views.signup,name='signup'),
    path('login/', accounts.views.loginview,name='login'),
]

login.html:
=======

<h1>Login</h1>

{% if error %}
{{ error }}

<br><br>
{% endif %}

<form method = "POST" action="{% url 'login' %}">
{% csrf_token %}
Username:
<br>
<input type="text" name="username" placeholder = "username">
<br>
Password:
<br>
<input type="password" name="password" placeholder="password">
<br>
<br>
<input type="submit" value ="Login">
</form>



--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c68b3563-aaf2-4e99-b6a6-ca0002f4fbecn%40googlegroups.com.

No comments:

Post a Comment