Django将url与双斜杠匹配,但并非没有

时间:2016-02-10 15:08:26

标签: python django django-urls

我有以下urls.py:

# ...imports...

urlpatterns = [
    url('', include('social.apps.django_app.urls', namespace='social')),
    url(r'^o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
    url(r'^admin/', admin.site.urls,),
    url('', include('main.urls', namespace="main")),
]

main.urls看起来像这样:

# ...imports...
urlpatterns = [
    url(r'^confirmation_email$', views.email_confirmation_sent, name="confirmation_email"),
    url(r'^oauth_callback$', views.oauth_callback, name="oauth_callback"),
    url(r'^.*', views.index, name='index')
]

我希望将/ oauth_callback路由到views.auth_callback,但它不起作用。另一方面,// oauth_callback在那里路由。似乎隐含匹配的初始斜杠被考虑了两次。

另一方面,`social.apps.django_app.urls',看起来像这样:

urlpatterns = [
    # authentication / association
    url(r'^login/(?P<backend>[^/]+){0}$'.format(extra), views.auth,
        name='begin'),
    url(r'^complete/(?P<backend>[^/]+){0}$'.format(extra), views.complete,
        name='complete'),
    # disconnection
    url(r'^disconnect/(?P<backend>[^/]+){0}$'.format(extra), views.disconnect,
        name='disconnect'),
    url(r'^disconnect/(?P<backend>[^/]+)/(?P<association_id>[^/]+){0}$'
        .format(extra), views.disconnect, name='disconnect_individual'),
]

它包含在我的主urls.py文件中,与我包含main.urls文件的方式完全相同,但转到/login/twitter的工作正常。除了一些格式差异,以及一些处理尾部斜杠的逻辑(模式结尾处的.format调用),我没有看到任何可能导致行为差异的事情。什么可能在这里造成麻烦?

修改

如果我手动设置了urlresolver并解析了路径/oauth_callback,它就能正常工作。

>>> from django.core import urlresolvers
>>> urlresolvers.get_urlconf()
>>> from django.conf import settings
>>> settings.ROOT_URLCONF
'podato.urls'
>>> urlresolvers.set_urlconf(settings.ROOT_URLCONF)
>>> urlresolvers.get_urlconf()
'podato.urls'    
>>> resolver = urlresolvers.get_resolver(settings.ROOT_URLCONF)
>>> resolver.resolve("/")
ResolverMatch(func=main.views.index, args=(), kwargs={}, url_name=index, app_names=[], namespaces=['main'])
>>> resolver.resolve("/oauth_callback")
ResolverMatch(func=main.views.oauth_callback, args=(), kwargs={}, url_name=oauth_callback, app_names=[], namespaces=['main'])

我已检查过request.path_info是否未更改。 Nore是改变urlconf的任何中间件。

0 个答案:

没有答案