django urlresolve无法解决

时间:2013-09-08 18:24:22

标签: django reverse django-urls

我已经尝试了很多东西,并阅读了文档,其他相关问题,我对此变得疯狂和盲目,因为它似乎很清楚错误在哪里,但我找不到它。

我的项目名称:'observatorio'。

我的应用名称:'tdcp'

我的目标结构如下:

observatorio/
   settings.py
   urls.py
   manage.py
   ...
   tdcp/
       static/
       templates/
       __init__.py
       urls.py
       views.py
       models.py

我的settings.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'),

)

我认为这个设置没问题,我可以访问我的任何页面,并且在我尝试解析模板标记中的网址之前没有问题。错误必须在项目urls.py中,当我打电话来包含应用程序urls.py时,因为我发现如果在我的settings.py中我将它设置为:

ROOT_URLCONF = 'tdcp.urls'

像这样工作!!,它渲染页面并解析我的模板中的任何url标记,buuuuut如果我设置如下:

# ROOT_URLCONF = 'urls'

因此使用include调用它会停止解析URL。

我发现了urlresolver的这个shell测试,我试着找出urlresolve在reverse()时使用的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', {})]}>

1 个答案:

答案 0 :(得分:2)

我自己是Django的新手,所以我对此并不是100%肯定,但我认为问题的一部分可能与你在项目urls.py中为include声明正则表达式的方式有关。

而不是

url('', include('tdcp.urls', namespace='tdcp'))

url(r'^/', include('tdcp.urls', namespace='tdcp'))

以下是Django文档中的相关摘录:

  

请注意,此示例中的正则表达式没有$(字符串结尾匹配字符),但包含尾部斜杠。每当Django遇到include()时,它会删除与该点匹配的URL的任何部分,并将剩余的字符串发送到包含的URLconf以进行进一步处理。

由于您传入了空的正则表达式,因此整个URL都匹配,因此没有任何内容可以传递给包含的URLConf。我不确定它会如何与反向旋转变压器一起发挥作用,但它也可能会破坏它。 (我快速浏览了Django代码,看看它是否是特殊情况下的空字符串,但没有看到它 - 我可能错过了它。)