页面未找到 (404) Django 问题!该怎么办?

huangapple go评论55阅读模式
英文:

Page not found (404) django problem!What to do?

问题

我的项目"mysite"中有一个名为"polls"的应用程序。

当我运行我的服务器时,我收到404错误。

以下是我的代码:

mysite/settings.py

INSTALLED_APPS = [
    'polls',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

mysite/urls.py

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

urlpatterns = [
    path("polls/", include("polls.urls")),
    path("admin/", admin.site.urls),
]

polls/views.py

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

polls/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path("", views.index, name="index"),
]

如何解决这个问题?

英文:

My project "mysite" has one app "polls" in it

When i run my server i receive 404 error:
页面未找到 (404) Django 问题!该怎么办?

Here is my code:

mysite/settings.py

INSTALLED_APPS = [
    'polls',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

mysite/urls.py

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

urlpatterns = [
    path("polls/", include("polls.urls")),
    path("admin/", admin.site.urls),
]

polls/views.py

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

polls/urls.py

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
]

How can I fix this problem?

答案1

得分: 1

在错误信息中,您可以看到投票(polls)和管理(admin)的URL已被正确设置并被Django所知。

但是:您发送了一个请求到"127.0.0.1:8000/",这不是您URL配置的一部分:

urlpatterns = [
    path("polls/", include("polls.urls")),
    path("admin/", admin.site.urls),
]

您可以选择要么调用

> 127.0.0.1:8000/polls

或者

> 127.0.0.1:8000/admin

如果您希望在根目录下看到投票应用程序,您需要更改为:

urlpatterns = [
    path("/", include("polls.urls")),
    path("admin/", admin.site.urls),
]
英文:

in the error message you can see that polls and admin urls are correctly setup and known by django.

But: you make a request to "127.0.0.1:8000/" which is not part of your url-conf:

urlpatterns = [
    path("polls/", include("polls.urls")),
    path("admin/", admin.site.urls),
]

You can either call

> 127.0.0.1:8000/polls

or

> 127.0.0.1:8000/admin

.

If you expect to see polls app at the root you need to change to:

urlpatterns = [
    path("/", include("polls.urls")),
    path("admin/", admin.site.urls),
]

huangapple
  • 本文由 发表于 2023年6月27日 18:19:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定