2017-04-21 54 views
0

餵我初學者學習的Django我triying遵循本教程:如何克服以下url問題?

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

我創建了一個叫做民意調查,以測試我的網站的應用程序:

因爲我沒有知道從哪裏把文件所謂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

urls.py定義所允許的URL模式你的項目。所以,在你的項目/ admin /和/ polls /允許的URL。所以,請嘗試http://127.0.0.1:8000/polls/或http://127.0.0.1:8000/admin/ –

+0

@SteephanSelvaraj非常感謝我試了一下,我得到了所需的輸出,默認只有一個問題只需鍵入http://127.0.0.1:8000/ Django應該將我重定向到索引? – neo33

+0

將'url(r'^ polls /',include('polls.urls'))'改爲'Django/mysite/mysite/url的url(r'',include('polls.urls'))。 py'。 因此,現在http://127.0.0.1:8000/將重定向到索引。 –

回答

1

的urls.py不進入的遷移文件夾中。

您是否在settings.py文件中註冊了您的投票應用程序?您需要將輪詢應用程序的名稱添加到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 
+1

非常感謝支持,我非常感謝幫助 – neo33