2015-08-26 46 views
2

我從官方文檔創建了一個項目。一切都一樣,但我仍然無法通過我的應用程序urls.py與我的根urls.py進行通信。根網址文件未與應用網址文件進行通信

如果我使用視圖重定向到app urls.py,它們不起作用。但是,如果使用root urls.py,它也可以正常工作。

我的設置是:

ROOT_URLCONF = 'mysite.urls' 

根網址:

from django.conf import settings 
from django.conf.urls.static import static 

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

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

] 

if settings.DEBUG: 
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

應用urls.py:我想提出

from django.conf.urls import url 

from polls import views 

urlpatterns = [ 
    # ex: /polls/ 
    url(r'^$', views.index, name='index'), 
    # ex: /polls/5/ 
    url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'), 
    # ex: /polls/5/results/ 
    url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'), 
    # ex: /polls/5/vote/ 
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'), 
] 

簡單的看法是:

from django.shortcuts import render, HttpResponse, RequestContext, loader 
from .models import Question 

def index(request): 
    latest_question_list = Question.objects.order_by('-pub_date')[:5] 
    context = {'latest_question_list': latest_question_list} 
    return render(request, 'index.html', context) 

請幫

polls/$取出美元后,我得到一個新的錯誤:

Page not found (404) 
Request Method: GET 
Request URL: http://localhost:8000/ 
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: 
^polls/ 
^admin/ 
^static\/(?P<path>.*)$ 
^media\/(?P<path>.*)$ 
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. 

回答

0

從被包括投票的網址鏈接中刪除美元符號。

url(r'^polls/', include('polls.urls')), 
+0

也試過這個,但沒有解決方案。刪除$後可以看到上面的錯誤。 –

+0

請勿放回美元符號。刪除它是正確的事情。現在,你得到錯誤是因爲你要'/',但是你還沒有爲'/'定義任何url模式。嘗試去'/ admin /',或'/ polls /',你已經添加了url模式。 – Alasdair