Django views.py函数没有被调用

kx1ctssn  于 2022-11-19  发布在  Go
关注(0)|答案(1)|浏览(204)

我想在用户点击登录按钮时调用函数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()的第一行中打印了一些内容,我也可以看到该函数从未被调用过。

ru9i0ody

ru9i0ody1#

从我这边设置工作正常。
在我的情况下,我使用了用户名而不是电子邮件。
===== views.py网站=========================================================================================================================================================================================

def login_user(request):
   if request.method == "POST":  # if user goes to webpage and fills out form, do something
       uname = request.POST['uname']
       password = request.POST['password']
       user = authenticate(request, username=uname, 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, 'index.html', {})

====项目/urls.py ====

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path("", include("myapp.urls")),
    path('admin/', admin.site.urls),

]

====我的应用程序/urls.py ====

from django.urls import path
from .views import *
 
urlpatterns = [
 
    path('', login_user,name='home'),
    path('login', login_user, name='login'),
 
]

=====索引. html

<div class="container my-5">
  <div class="row">
    <div class="col-5">
      <h2>Log In {{request.user}}</h2>
      <form method="POST">
        {% csrf_token %}
        <div class="mb-3">
          <label for="email" class="form-label">Email address</label>
          <input type="text" class="form-control" name="uname" 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>
    </div>
  </div>

========输出========

相关问题