根URL文件未与app url文件通信

时间:2015-08-26 22:17:08

标签: python django

我已经从官方文档创建了一个项目。一切都一样,但我仍然无法通过我的app urls.py与我的root urls.py通信。

如果我使用视图重定向到app urls.py它们不起作用。但是如果使用root urls.py也一样。它可以正常工作。

我的设置是:

ROOT_URLCONF = 'mysite.urls'

root 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)

app 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 个答案:

没有答案