英文:
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:
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),
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论