سؤال

I just found out that the following code gives the same result:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
)

as this one:

urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),
)

So, what is the utility of using the url() function in the first way of coding?

هل كانت مفيدة؟

المحلول

Currently these two options are essentially the same, since in the second case Django applies url() function for each of your tuples under the hood. Quote from the django.conf.url.patterns source code:

def patterns(prefix, *args):
    ...
    pattern_list = []
    for t in args:
        if isinstance(t, (list, tuple)):  # < HERE
            t = url(prefix=prefix, *t)
        elif isinstance(t, RegexURLPattern):
            t.add_prefix(prefix)
        pattern_list.append(t)
    return pattern_list

Also, as @Kevin Christopher Henry already noted here, urlpatterns is becoming deprecated (see Deprecate 'prefix' arg to django.conf.urls.patterns ticket and this pull request). Without having urlpatterns, we'll have to explicitly call url() on each of the items of the url list. Better get into the habit of using it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top