'Django users can't login after I create an account but admin can and i didnt create any registration form by only manually can add users
I've created a login page as a homepage and only by adding manually in Django administration can create a user. I didn't create a registration form and message error else statement in home viwes.py is working on the admin panel. If I login by user, I get CSRF token error
homepage views.py
from django.shortcuts import redirect, render
from loginapp.models import Register
from django.contrib import messages
from django.contrib.auth import authenticate, login , logout
def home(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(request,username=username, password=password)
if user is not None:
login(request, user)
return render(request, "home.html")
else:
messages.error(request, "Bad Creds!!")
return redirect('/')
return render(request, "home.html")
login page views.py
def index(request):
return render(request, "index.html")
login page name as index.html
<form id="stripe-login" method="POST" action="{% url 'home' %}">
{% csrf_token %}
<div class="trick" style="padding-bottom: 10px;">
<input type="text" name="username" required>
<label for="email">Username</label>
</div>
<div class=" trick" style="padding-bottom:10px;">
<input type="password" name="password" required>
<label for="password">Password</label>
</div>
<div class="field" style="padding-bottom:10px; padding-top: 10px;">
<input type="submit" name="submit" value="Continue">
</div>
<div class="reset-pass" style="padding-top: 20px;">
<a href="#">Forgot your password?</a>
</div>
<div class="footer-link text-light" style="padding-top:10px;">
<span>
Don't have an account?
<a href="{% url 'signup' %}">
Request for a new account
</a>
</span>
</div>
</form>
Solution 1:[1]
for creating user you must use User.objects.create_user() method Ex: User.objects.create_user(username=username,email=email,password=password)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | shahriar khan |
