我在这里遵循本指南tango with django,我正在阅读/复制tutorial part 3
但我无法获得不同的观点来展示不同的内容。我错过了/某种地方的解释。
所以这是我的项目urls.py
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'Sayth.views.home', name='home'),
# url(r'^Sayth/', include('Sayth.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^rango/', include('rango.urls')),
url(r'^rango/about/', include('rango.urls')),
)
这是我的应用程序urls.py
from django.conf.urls import patterns, url
from rango import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^$', views.about, name='about'),
)
这些是我的观点
# Create your views here.
from django.http import HttpResponse
def index(request):
return HttpResponse("Rango says Hello World! <a href='/rango/about'>About</a>" )
def about(request):
return HttpResponse("This is the about page. <a href='/rango/'>Index</a>")
我的两个页面都解析但都显示索引页面,为什么?
答案 0 :(得分:1)
定义的网址不正确。根urls.py应该只有一个指向应用程序的指针,应用程序的urls.py应该解析不同的端点。
尝试以下urls.py:
#Project urls.py
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'Sayth.views.home', name='home'),
# url(r'^Sayth/', include('Sayth.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^rango/', include('rango.urls')),
)
#rango/urls.py
from django.conf.urls import patterns, url
from rango import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='about'),
)
答案 1 :(得分:1)
正如第一个回答所提到的,你的项目的urls.py只需要一个指向应用程序urls.py的指针。
从概念上讲,就像这样,任何匹配'^ rango /'的东西都应传递到应用程序urls.py,然后应用程序处理其余的url ..'/'或'about /'
因此,在您的应用程序urls.py中,您需要确保存在每种模式的映射。
在tangowithdjango书中,about页面是一个练习,留给读者,但如果你有将来的问题,你可以随时检查应用程序的代码,例如:
项目urls.py:
应用程序urls.py:
https://github.com/leifos/tango_with_django/blob/master/tango_with_django_project/rango/urls.py
答案 2 :(得分:0)
如果您不想在应用上创建urls.py,您可以像这样进行网址映射:
urlpatterns = patterns('',
# ...
# most detailed urls comes first.
url(r'^rango/about/?$', 'rango.views.about', name='about'),
url(r'^rango/?$', 'rango.views.index', name='index'),
)