如何克服以下网址问题?

时间:2017-04-21 16:01:21

标签: django python-3.x

您好我是初学者学习Django我正在试图学习本教程:

https://docs.djangoproject.com/en/1.11/intro/tutorial01/

我创建了一个名为Polls的应用来测试我的网站:

因为我不知道在哪里放置名为urls.py的文件 我把这个文件放在以下目录中:

Django/mysite/polls/urls.py
Django/mysite/polls/migrations/urls.py

此文件包含:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

最后我添加了以下几行:

在这个级别:

Django/mysite/mysite/urls.py

此文件包含:

from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls import url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^polls/', include('polls.urls')),

]

我不知道问题出在哪里,但是当我跑步时:

Django/mysite$ python3 manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

April 21, 2017 - 15:59:43
Django version 1.10.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

我进入了以下页面:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^polls/
The current URL, , 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.

所以我非常感谢支持克服这个问题, 感谢您的支持

1 个答案:

答案 0 :(得分:1)

urls.py没有进入迁移文件夹。

您是否在settings.py文件中注册了自己的投票应用?您需要将Polls应用程序的名称添加到INSTALLED_APPS列表中。

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'example: add app name here', 
'polls',]

然后,您需要在民意调查应用中创建一个url.py文件。

polls/
     urls.py

在该文件中,您将添加:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

您要在浏览器中使用的网址将是这样的:

 http://127.0.0.1:8000/polls