覆盖Django中的urlpattern?

时间:2014-01-07 14:57:56

标签: python django python-2.7

尝试为某些Django的应用程序视图更改url,但替换urlpatterns列表似乎没有效果:

from django.core.urlresolvers import reverse
from myapp.contact.urls import urlpatterns
from django.conf.urls.defaults import patterns

print [u for u in urlpatterns if 'user_account_info' == str(getattr(u,'name',''))]
# [<RegexURLPattern user_account_info ^$>]
print reverse('user_account_info', current_app='myapp.contact.views'
# '/accounts/'

# Create url pattern replacement
patt=patterns('myapp.contact.views', (r'^konto/$', 'view', {}, 'user_account_info') )
print patt
# [<RegexURLPattern user_account_info ^konto/$>]

for ix in range(len(urlpatterns)):
    if hasattr(urlpatterns[ix], 'name') and \
            urlpatterns[ix].name == 'user_account_info':
        del urlpatterns[ix]
        urlpatterns.insert(0, patt)
        print 'done !'

print [u for u in urlpatterns if 'user_account_info' == str(getattr(u,'name',''))]
# [<RegexURLPattern user_account_info ^konto/$>]
# seems replacement was made ^^^^^^^^^^^^^^^^^

print reverse('user_account_info', current_app='myapp.contact.views'
# '/accounts/'

为什么reverse仍会返回旧路径?有什么想法吗?

0 个答案:

没有答案