So I thought when I press submit on the form (login page), that it would trigger my loginPage View and hit the request.POST == 'Method'
becuase the form method is post but it is not doing this and I cannot login.
Bit confused since this worked before and I haven't touched it since. Any ideas guys?
I have a html login page called login_register.html
. Below,
<form method="POST" action="">
{% csrf_token %}
<div >
<input type="text" name="username" placeholder="Username" autocomplete="off"></input>
<input type="password" name="password" placeholder="Password" autocomplete="off"></input>
<input type="submit" value="login"></input>
</div>
</form>
I have the urls.py file as well. Below,
path('login/', views.loginPage, name='login'),
And lastly, I have the View. Below,
from django.contrib.auth.models import User
def loginPage(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
try:
user = User.objects.get(username = user)
except:
print("Error - user / password incorrect, does not exist.")
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
print("User does not exist.")
return render(request, 'base/login_register.html')
CodePudding user response:
You should complete the action
attribute in the form:
<form method="POST" action="{% url 'login' %}">
Note: the URL name can also be 'appname:login'. It depends on your URL setup.
CodePudding user response:
I ended up solving the issue. The search bar component was causing the issue. After playing around with it, I ended up moved it around in the folder and now it all works. I think this is the result because before it was in the main.html where everything was static like links text etc. I moved it into my home.html where everything is dynamic and now it works.
Thanks everyone for your help. This was quite an odd one.