try to authenticate login, submit nothing happened
Question:
login.html in (authenticate/login.html)
{% extends "events/base.html" %}
{% block content %}
<h1>Login</h1>
<br><br>
<form action="" method="POST">
{% csrf_token %}
<form>
<div class="mb-3">
<label for="exampleInputUserName" class="form-label">User name</label>
<input type="text" class="form-control" name="username">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Password</label>
<input type="password" class="form-control" name="password">
</div>
</form>
<input type="submit" value="Submit" class="btn btn-secondary">
</form>
<br><br>
{% endblock content %}
when click the submit button nothing happened
urls.py in (events)
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
urls.py in (members)
from django.urls import path
from . import views
urlpatterns = [
path('login_user/', views.login_user, name='login'),
]
urls.py in (myclub_webesite)
rom django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('events.urls')),
path('members/', include('django.contrib.auth.urls')),
path('members/', include('members.urls')),
]
views.py in (members)
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
def login_user(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
messages.success(
request, ("There Was An Error Logging In, Try Again..."))
return redirect('login')
else:
return render(request, 'authenticate/login.html', {})
setting are put in members
i have try use other code
username = request.POST[‘username’]
password = request.POST[‘password’]
and same nothing happened
Answers:
In your form tag, define the appropriate API method:
<form method='POST'>
.
.
.
</form>
<input type="submit" class="btn btn-secondary">
### You have used 2 form tag so there was an error
{% extends "events/base.html" %}
{% block content %}
<h1>Login</h1>
<br><br>
<form action="" method="POST">
{% csrf_token %}
<div class="mb-3">
<label for="exampleInputUserName" class="form-label">User name</label>
<input type="text" class="form-control" name="username">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Password</label>
<input type="password" class="form-control" name="password">
</div>
<input type="submit" value="Submit" class="btn btn-secondary">
</form>
<br><br>
{% endblock content %}
Views.py I change the success message to error message because if there is an error then user so will see error message not success
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
def login_user(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
### success -> error
messages.error(
request, ("There Was An Error Logging In, Try Again..."))
return redirect('login')
else:
return render(request, 'authenticate/login.html', {})
login.html in (authenticate/login.html)
{% extends "events/base.html" %}
{% block content %}
<h1>Login</h1>
<br><br>
<form action="" method="POST">
{% csrf_token %}
<form>
<div class="mb-3">
<label for="exampleInputUserName" class="form-label">User name</label>
<input type="text" class="form-control" name="username">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Password</label>
<input type="password" class="form-control" name="password">
</div>
</form>
<input type="submit" value="Submit" class="btn btn-secondary">
</form>
<br><br>
{% endblock content %}
when click the submit button nothing happened
urls.py in (events)
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
urls.py in (members)
from django.urls import path
from . import views
urlpatterns = [
path('login_user/', views.login_user, name='login'),
]
urls.py in (myclub_webesite)
rom django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('events.urls')),
path('members/', include('django.contrib.auth.urls')),
path('members/', include('members.urls')),
]
views.py in (members)
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
def login_user(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
messages.success(
request, ("There Was An Error Logging In, Try Again..."))
return redirect('login')
else:
return render(request, 'authenticate/login.html', {})
setting are put in members
i have try use other code
username = request.POST[‘username’]
password = request.POST[‘password’]
and same nothing happened
In your form tag, define the appropriate API method:
<form method='POST'>
.
.
.
</form>
<input type="submit" class="btn btn-secondary">
### You have used 2 form tag so there was an error
{% extends "events/base.html" %}
{% block content %}
<h1>Login</h1>
<br><br>
<form action="" method="POST">
{% csrf_token %}
<div class="mb-3">
<label for="exampleInputUserName" class="form-label">User name</label>
<input type="text" class="form-control" name="username">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Password</label>
<input type="password" class="form-control" name="password">
</div>
<input type="submit" value="Submit" class="btn btn-secondary">
</form>
<br><br>
{% endblock content %}
Views.py I change the success message to error message because if there is an error then user so will see error message not success
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
def login_user(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
### success -> error
messages.error(
request, ("There Was An Error Logging In, Try Again..."))
return redirect('login')
else:
return render(request, 'authenticate/login.html', {})