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

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

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

问题

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

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

以下是我的代码:

mysite/settings.py

  1. INSTALLED_APPS = [
  2. 'polls',
  3. 'django.contrib.admin',
  4. 'django.contrib.auth',
  5. 'django.contrib.contenttypes',
  6. 'django.contrib.sessions',
  7. 'django.contrib.messages',
  8. 'django.contrib.staticfiles',
  9. ]

mysite/urls.py

  1. from django.contrib import admin
  2. from django.urls import include, path
  3. urlpatterns = [
  4. path("polls/", include("polls.urls")),
  5. path("admin/", admin.site.urls),
  6. ]

polls/views.py

  1. from django.http import HttpResponse
  2. def index(request):
  3. return HttpResponse("Hello, world. You're at the polls index.")

polls/urls.py

  1. from django.urls import path
  2. from . import views
  3. urlpatterns = [
  4. path("", views.index, name="index"),
  5. ]

如何解决这个问题?

英文:

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

  1. INSTALLED_APPS = [
  2. 'polls',
  3. 'django.contrib.admin',
  4. 'django.contrib.auth',
  5. 'django.contrib.contenttypes',
  6. 'django.contrib.sessions',
  7. 'django.contrib.messages',
  8. 'django.contrib.staticfiles',
  9. ]

mysite/urls.py

  1. from django.contrib import admin
  2. from django.urls import include, path
  3. urlpatterns = [
  4. path("polls/", include("polls.urls")),
  5. path("admin/", admin.site.urls),
  6. ]

polls/views.py

  1. from django.http import HttpResponse
  2. def index(request):
  3. return HttpResponse("Hello, world. You're at the polls index.")

polls/urls.py

  1. from django.urls import path
  2. from . import views
  3. urlpatterns = [
  4. path("", views.index, name="index"),
  5. ]

How can I fix this problem?

答案1

得分: 1

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

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

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

您可以选择要么调用

> 127.0.0.1:8000/polls

或者

> 127.0.0.1:8000/admin

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

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

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:

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

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:

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

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:

确定