python - 转到 "http://127.0.0.1:8000/restaurant/sign-in/"页面时出现页面未找到 (404) 错误

标签 python django django-2.2

当我转到“http://127.0.0.1:8000/restaurant/sign-in/”时,我收到页面未找到(404)错误。但我可以通过“http://127.0.0.1:8000/restaurant/$”访问主页。

我也尝试过“http://127.0.0.1:8000/restaurant/sign-in/ $”,但这也给了我错误(init() 接受 1 个位置参数,但给出了 2 个)。

我的 urls.py 是

from django.contrib import admin
from django.urls import path
from foodtaskerapp import views
from django.contrib.auth import views as auth_views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('restaurant/sign-in/$', auth_views.LoginView,
         {'template_name': 'restaurant/sign_in.html'},
         name='restaurant-sign-in'),
    path('restaurant/sign-out', auth_views.LogoutView,
         {'next_page': '/'},
         name='restaurant-sign-out'),
    path('restaurant/$', views.restaurant_home, name='restaurant- 
         home'),
]

我的views.py是

from django.shortcuts import render, redirect

def home(request):
    return redirect(restaurant_home)

def restaurant_home(request):
    return render(request, 'restaurant/home.html', {})

here is the screenshot of the error

我也有

<body>
    <form method="POST">
        {% csrf_token %}
        {{ form }}
        <button type="submit">Sign In</button>
    </form>
</body>

在sign_in.html中,但表单未显示,仅显示登录。 only sign is shown but not the form

最佳答案

您正在使用的函数的语法在最新版本的 django 中已更改,这就是您收到该错误的原因(您共享的代码位于 code4startups 制作的教程中,该教程使用旧版本的 django)。 您应该将路径命令修改为:

path('restaurant/sign-in/', auth_views.LoginView.as_view(
       template_name='restaurant/sign_in.html'),
       name='restaurant-sign-in'),

关于python - 转到 "http://127.0.0.1:8000/restaurant/sign-in/"页面时出现页面未找到 (404) 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55989109/

相关文章:

python - 服务器未在 hello world 应用程序中启动

python - 尝试将不同的项目附加到列表中

Python - 缓存一个属性以避免 future 的计算

python - Django 使用序列化器在 Restframework 中进行 API 调用的一对多关系

python - 更新到 Django 2.2.4 和 python 3.7 (toml.py) 后无法运行 mange.py runserver

python - 带有测试语句的 while 循环的惯用 Python 版本的 Ruby 代码?

python - 通过 Web 客户端连接到服务器时出现 SSL 握手错误

django - 没有模块名为六

python - 在 Django 中打开 zip 文件中存储的照片时出错

python - 无法在 Django 中调试或使用 pdb : bdb. BdbQuit