我已经尝试了很多东西,并阅读了文档,其他相关问题,我对此变得疯狂和盲目,因为似乎很清楚错误在哪里,但我找不到它。
我的项目名称:'observatorio'。
我的应用名称:'tdcp'
我的目录结构是这样的:
observatorio/
settings.py
urls.py
manage.py
...
tdcp/
static/
templates/
__init__.py
urls.py
views.py
models.py
我的一些设置.py:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'core',
'tdcp',
'south',
'django.contrib.humanize',
)
ROOT_URLCONF = 'urls'
# ROOT_URLCONF = 'tdcp.urls'
我想在项目 urls.py 中包含我的应用程序 tdcp\urls.py 以便在我将来创建更多应用程序时做正确的事情,并从一开始就拥有更具可扩展性的代码。所以:
我的项目 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('',
url('', include('tdcp.urls', namespace='tdcp')),
# 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)),
)
我的 tdcp.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('tdcp.views',
url(r'^$', 'index', name='index'),
url(r'^prueba$', 'prueba', name='prueba'),
url(r'^all_urls_view', 'all_urls_view', name='all_urls_view'),
url(r'^(?P<year>\d+)/(?P<metric>\w+)$', 'country_metric_view', name='country_metric_view'),
url(r'^(?P<year>\d+)/(?P<province_code>\d+)/(?P<type>\w+)/(?P<metric>\w+)$', 'one_province_metric_view', name='one_province_metric_view'),
url(r'^(?P<year>\d+)/(?P<ine_code>\w+)/(?P<metric>\w+)$', 'one_aapp_metric_view', name='one_aapp_metric_view'),
url(r'^api/autocomplete_publicadministrations/$', 'autocomplete_publicadministrations', name='autocomplete_publicadministrations'),
url(r'^api/autocomplete_indicators/', 'autocomplete_indicators', name='autocomplete_indicators'),
url(r'^api/autocomplete_provinces/', 'autocomplete_provinces', name='autocomplete_provinces'),
url(r'^navigation_form/', 'navigation_form', name='navigation_form'),
)
我认为这个设置还可以,我可以访问我的任何页面,并且在我尝试解析模板标签中的 url 之前没有问题。错误必须在项目 urls.py 中,当我调用以包含应用程序 urls.py 时,因为我发现如果在我的 settings.py 中我将其设置为:
ROOT_URLCONF = 'tdcp.urls'
像这样它可以工作!!,它呈现页面并解析我的模板中的任何 url 标签,如果我设置如下:
# ROOT_URLCONF = 'urls'
因此使用 include 调用它会停止解析 url。
我找到了这个 urlresolver 的 shell 测试,并尝试找出在 reverse() 中进行 urlresolve 时使用的 ulrs 字典。当然,这两种情况都会发生:
使用 ROOT_URLCONF = 'tdcp.urls'
>>> from django.core import urlresolvers
>>> from tdcp import views
>>> print urlresolvers.get_resolver(None).reverse_dict
<MultiValueDict: {'index': [([(u'', [])], '$', {})], 'autocomplete_publicadministrations': [([(u'api/autocomplete_publicadministrations/', [])], 'api/autocomplete_publicadministrations/$', {})], 'autocomplete_provinces': [([(u'api/autocomplete_provinces/', [])], 'api/autocomplete_provinces/', {})], 'prueba': [([(u'prueba', [])], 'prueba$', {})], <function country_metric_view at 0x02004670>: [([(u'%(year)s/%(metric)s', [u'year', u'metric'])], '(?P<year>\\d+)/(?P<metric>\\w+)$', {})], 'one_province_metric_view': [([(u'%(year)s/%(province_code)s/%(type)s/%(metric)s', [u'year', u'province_code', u'type', u'metric'])], '(?P<year>\\d+)/(?P<province_code>\\d+)/(?P<type>\\w+)/(?P<metric>\\w+)$', {})], <function one_province_metric_view at 0x020046B0>: [([(u'%(year)s/%(province_code)s/%(type)s/%(metric)s', [u'year', u'province_code', u'type', u'metric'])], '(?P<year>\\d+)/(?P<province_code>\\d+)/(?P<type>\\w+)/(?P<metric>\\w+)$', {})], 'one_aapp_metric_view': [([(u'%(year)s/%(ine_code)s/%(metric)s', [u'year', u'ine_code', u'metric'])], '(?P<year>\\d+)/(?P<ine_code>\\w+)/(?P<metric>\\w+)$', {})], <function one_aapp_metric_view at 0x020046F0>: [([(u'%(year)s/%(ine_code)s/%(metric)s', [u'year', u'ine_code', u'metric'])], '(?P<year>\\d+)/(?P<ine_code>\\w+)/(?P<metric>\\w+)$', {})], 'navigation_form': [([(u'navigation_form/', [])], 'navigation_form/', {})], <function index at 0x02004630>: [([(u'', [])], '$', {})], <function all_urls_view at 0x01A917B0>: [([(u'all_urls_view', [])], 'all_urls_view', {})], <function autocomplete_provinces at 0x02004530>: [([(u'api/autocomplete_provinces/', [])], 'api/autocomplete_provinces/', {})], <function prueba at 0x020045F0>: [([(u'prueba', [])], 'prueba$', {})], 'autocomplete_indicators': [([(u'api/autocomplete_indicators/', [])], 'api/autocomplete_indicators/', {})], <function autocomplete_indicators at 0x02004570>: [([(u'api/autocomplete_indicators/', [])], 'api/autocomplete_indicators/', {})], <function navigation_form at 0x020045B0>: [([(u'navigation_form/', [])], 'navigation_form/', {})], 'country_metric_view': [([(u'%(year)s/%(metric)s', [u'year', u'metric'])], '(?P<year>\\d+)/(?P<metric>\\w+)$', {})], 'all_urls_view': [([(u'all_urls_view', [])], 'all_urls_view', {})], <function autocomplete_publicadministrations at 0x01FFC5F0>: [([(u'api/autocomplete_publicadministrations/', [])], 'api/autocomplete_publicadministrations/$', {})]}>
>>> print urlresolvers.get_resolver(None).reverse_dict.items()[0][0]
index
ROOT_URLCONF = 'urls'
>>> from django.core import urlresolvers
>>> from tdcp import views
>>> print urlresolvers.get_resolver(None).reverse_dict <MultiValueDict: {}>
>>> print urlresolvers.get_resolver(None).reverse_dict.items()[0][0] Traceback (most recent call last): File "<console>", line 1, in <module> IndexError: list index out of range
我究竟做错了什么??
再试一次
I updated the project urls.py like this:
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('',
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
# project urls
url(r'^hola', 'hola', name='hola'),
# include app urls
url(r'^/', include('tdcp.urls', namespace='tdcp')),
)
所以现在,当我在 shell 中打印 urlresolve 字典时,它只会找到挂起 urls.py 项目的唯一静态 url,没有包含的 urls.py,无论是 admin/ 还是我的应用程序 tdcp/
>>> from django.core import urlresolvers
>>> from tdcp import views
>>> print urlresolvers.get_resolver(None).reverse_dict
<MultiValueDict: {'hola': [([(u'hola', [])], 'hola', {}), ([(u'hola', [])], 'hola', {})]}>