我想在用户点击登录按钮时调用函数login_user()。但是这个函数从来没有被调用过。我想我已经正确地设置了所有的URLConfs。你能发现我的错误吗?
views.py
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
# Create your views here.
def login_user(request):
if request.method == "POST": # if user goes to webpage and fills out form, do something
email = request.POST['email']
password = request.POST['password']
user = authenticate(request, email=email, password=password)
if user is not None:
login(request, user)
return redirect('home') # Redirect to a success page.
else:
messages.success(request, "There was an Error Logging In, please try again.")
return redirect('login')
else:
return render(request, 'registration/login.html', {})
urls.py (main)
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('loginForm.urls')),
path('accounts/', include('django.contrib.auth.urls')),
]
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('login', views.login_user, name='login'),
]
login.html
<h2>Log In</h2>
<form method="POST">
{% csrf_token %}
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" name="email" aria-describedby="emailHelp">
</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="Log In" class="btn btn-secondary">
</form>
注意:即使我在login_user()的第一行中打印了一些内容,我也可以看到该函数从未被调用过。
1条答案
按热度按时间ru9i0ody1#
从我这边设置工作正常。
在我的情况下,我使用了用户名而不是电子邮件。
===== views.py网站=========================================================================================================================================================================================
====项目/urls.py ====
====我的应用程序/urls.py ====
=====索引. html
========输出========