Deprecate 'prefix' arg to django.conf.urls.patterns
|Reported by:||Carl Meyer||Owned by:||Tim Graham|
|Cc:||loic@…, Simon Charette||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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
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.)
Change History (10)
comment:1 Changed 3 years ago by
|Triage Stage:||Unreviewed → Accepted|
|Version:||1.6 → master|