#22218: Deprecate 'prefix' arg to django.conf.urls.patterns
------------------------------------------------+------------------------
               Reporter:  carljm                |          Owner:  nobody
                   Type:  Cleanup/optimization  |         Status:  new
              Component:  Core (URLs)           |        Version:  1.6
               Severity:  Normal                |       Keywords:
           Triage Stage:  Unreviewed            |      Has patch:  0
    Needs documentation:  0                     |    Needs tests:  0
Patch needs improvement:  0                     |  Easy pickings:  0
                  UI/UX:  0                     |
------------------------------------------------+------------------------
 In the olden days of Django, it was encouraged to reference views as
 strings in url patterns:

 {{{
 urlpatterns = patterns('',
     url('^$', 'myapp.views.myview'),
 )
 }}}

 and Django would magically import "myapp.views.myview" internally and turn
 the string into a real function reference.

 In order to reduce repetition when referencing many views from the same
 module, the `patterns` function takes a required initial "prefix" arg,
 which is prepended to all views-as-strings in that set of url patterns:

 {{{
 urlpatterns = patterns('myapp.views',
     url('^$', 'myview'),
     url('^other/$', 'otherview'),
 )
 }}}

 In the modern era, we have updated the tutorial to instead recommend
 actually importing your views module and referencing your view functions
 (or classes) directly. This has a number of advantages, all deriving from
 the fact that we are now using Normal Python in place of Django String
 Magic: the errors when you mistype a view name are less obscure, IDEs can
 help with autocompleting view names, etc.

 With the advent of the class-based generic views, it is even more likely
 that users are referencing view callables directly in their urlconf, due
 to the need to call `.as_view()`.

 So these days, the above use of the prefix arg is much more likely to be
 written (and is better written) as:

 {{{
 from myapp import views

 urlpatterns = patterns('',
     url('^$', views.myview),
     url('^other/$', views.otherview),
 )
 }}}

 This leaves the 'prefix' arg in modern Django code as a useless empty
 string that has to be passed to every invocation of `patterns`. This is an
 ugly API wart, and a burden when teaching new users (answering the
 newbie's question "why do I need this empty string as the first argument
 to patterns?" requires either a time-wasting detour into the history of
 Django URLconfs or a hand-wavy "don't worry, just do it").

 I suggest that we deprecate this argument. This will require type-checking
 the first argument to `patterns` for the duration of the deprecation
 period, but I don't think that's so bad.

 (I would not be opposed to deprecating the entire views-as-strings
 feature, but the additional gain there is only code we can remove and stop
 maintaining, not a benefit to the public API, so I decided to keep this
 proposal minimal. If there's consensus to deprecate the entire feature, I
 will happily update the summary to reflect that.)

-- 
Ticket URL: <https://code.djangoproject.com/ticket/22218>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.75c9506564418e6e7c9d6a73dc00c469%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to