gpt4 book ai didi

javascript - 登录 Python 和 Django 应用程序时本地服务器出现错误

转载 作者:行者123 更新时间:2023-12-02 22:40:53 25 4
gpt4 key购买 nike

我希望我的 login.html 页面首先作为我的 Python 和 Django 应用程序中的登录页面。但是,当我在填写用户名和密码后按下回车按钮时,服务器上项目的 urls.py 文件中就会显示错误。

项目的URLS.py文件:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
path('', include('tabs_app.urls')),
path('admin/', admin.site.urls),
path('accounts/', include('accounts.urls'))
]

index.html文件所在项目中应用程序的VIEWS.py文件:


from django.shortcuts import render

# Create your views here.
def login(request):
return render(request, 'login.html')

index.html文件所在项目中应用程序的URLS.py文件:

from django.urls import path
from . import views

urlpatterns = [
path('', views.login, name='login')
]

编写LOGIN代码的应用程序的VIEWS.py文件:

from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.models import User, auth

# Create your views here.

def login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']

user = auth.authenticate(username=username,password=password)

if user is not None:
auth.login(request, user)
return redirect('/')

else:
messages.info(request, 'invalid credentials')
return redirect('login')

else:
return render(request, 'login.html')

编写LOGIN代码的应用程序的URLS.py文件:

from django.urls import path

from . import views

urlpatterns = [
path('login', views.login, name='login')
]

本地服务器上出现错误消息:

Page not found (404)
Request Method: POST
Request URL: http://127.0.0.1:8000/login
Using the URLconf defined in tabs_pilot.urls, Django tried these URL patterns, in this order:
1. [name='login']
2. admin/
3. accounts/
^media/(?P<path>.*)$
The current path, login, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

最佳答案

您是否正在尝试使用 Django 的内置登录功能?在这种情况下,您的端点是 http://127.0.0.1:8000/accounts/login并且您的模板应该可以在模板目录的注册目录中找到。

Mozilla 在其 Django 教程中很好地概述了这一切是如何工作的。看看here .

关于javascript - 登录 Python 和 Django 应用程序时本地服务器出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58591464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com