【问题标题】:Django Page not found (404) but URLs are correct找不到 Django 页面 (404) 但 URL 正确
【发布时间】:2020-06-01 04:20:16
【问题描述】:

我遇到了这样的错误,尝试打开网址http://127.0.0.1:8000/account/时出现404错误

# apps/account/urls.py

from django.urls import path
from django.contrib.auth.views import LoginView
from . import views

urlpatterns = [
    path('', views.index),
    path('login/', LoginView.as_view(template_name='account/login.html'), name="login")
]

最奇怪的是,如果我添加类似 qwe/ 在路径中 http://127.0.0.1:8000/account/qwe 它会工作,并且第二个路径 login/ 正在工作

谁能告诉我问题出在哪里?

#apps/account/views.py

from django.shortcuts import render, HttpResponse


def index(request):
    return HttpResponse('<h1>Hello</h1>')

主 urls.py

#project/urls.py

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

urlpatterns = [
                  path('admin/', admin.site.urls),
                  path('', include("apps.main.urls")),
                  path('account/', include("apps.account.urls")),
              ]

Screenshot

【问题讨论】:

  • "apps.account.urls" 这应该是 "apps.accounts.urls"
  • 您的应用名称是帐户,并且您使用的是“apps.account.urls”而不是“apps.accounts.url”
  • 能否显示错误截图
  • 我为我在问题中的错字道歉,我已经更正了。名为“帐户”的应用程序
  • 哪个 django 版本?

标签: python django


【解决方案1】:

不确定这是否可行,但我建议您在 account views.py 中尝试不同的导入。

而不是下面。

#apps/account/views.py 
from django.shortcuts import render, HttpResponse

试试下面

#apps/account/views.py
from django.shortcuts import render
from django.http import HttpResponse

【讨论】:

  • 问题是,如果我将路径更改为path('qwe/', views.index) 或其他内容,那么一切正常。但是当路径为空时返回 404
  • 是否打开。注意最后没有/。 127.0.0.1:8000/account
【解决方案2】:

据我所知,您正在将页面作为主页。

urlpatterns = [
    path('', views.index),
    path('login/', LoginView.as_view(template_name='account/login.html'), name="login")
]

这里把第一个路径改成

path('index/',views.index),

那就试试吧。这可能有效,因为在您的代码中,您将两条路径指向主页,因此无论何时包含

http://127.0.0.1:8000/account/qwe and path('qwe/', views.index)

索引页通过路径 qwe 指向索引页。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 2016-08-01
    • 2015-10-22
    • 2014-03-20
    • 2020-10-27
    相关资源
    最近更新 更多