首页 > 解决方案 > 错误:无法路由到我新创建的应用程序

问题描述

因此,我正在关注https://docs.djangoproject.com/en/2.2/topics/http/urls/上的 django 教程, 我已经到了应该能够导航到投票 url 的地步,但我得到了 404 错误

Page not found (404)
Request Method:     GET
Request URL:    http://localhost:8000/polls/

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

    admin/

The current path, polls/, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

我的文件夹结构是

D:.
|   db.sqlite3
|   manage.py
|
+---mysite
|   |   settings.py
|   |   urls.py
|   |   wsgi.py
|   |   __init__.py
|   |
|   \---__pycache__
|           settings.cpython-37.pyc
|           urls.cpython-37.pyc
|           wsgi.cpython-37.pyc
|           __init__.cpython-37.pyc
|
\---polls
    |   admin.py
    |   apps.py
    |   models.py
    |   tests.py
    |   urls.py
    |   views.py
    |   __init__.py
    |
    \---migrations
            __init__.py

我已将 mysite/urls.py 修改为

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

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

在 polls 文件夹中添加了一个 urls.py 文件并用

from django.urls import path

from . import views

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

最后将 polls/views.py 文件调整为

from django.shortcuts import render
from django.http import HttpResponse


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

管理页面工作正常,但我无法导航到http://localhost:8000/polls/。我用不同的名字尝试了几次,但没有成功。请帮助。谢谢

标签: pythondjango

解决方案


它现在正在工作。我认为我对文件所做的更改之前没有保存。在我重新启动 atom 并保存所有文件后,它现在可以工作了。感谢您的帮助。


推荐阅读