diff options
| author | Tim Graham <timograham@gmail.com> | 2014-04-01 20:46:34 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-04-03 07:28:10 -0400 |
| commit | d73d0e071c1b4c86d57994a0ab55a74cfe80cdf5 (patch) | |
| tree | f36eebaf1cdd8bd07503b3a538e7d875ff8d29b4 /docs | |
| parent | e6ced2bb086396b57601d04ad5b3ba347d1eb785 (diff) | |
Fixed #22218 -- Deprecated django.conf.urls.patterns.
Thanks Carl Meyer for the suggestion and Alex Gaynor and Carl for reviews.
Diffstat (limited to 'docs')
28 files changed, 433 insertions, 387 deletions
diff --git a/docs/howto/static-files/index.txt b/docs/howto/static-files/index.txt index f94e2601e4..39e5cf1b94 100644 --- a/docs/howto/static-files/index.txt +++ b/docs/howto/static-files/index.txt @@ -94,9 +94,9 @@ this by adding the following snippet to your urls.py:: from django.conf import settings from django.conf.urls.static import static - urlpatterns = patterns('', + urlpatterns = [ # ... the rest of your URLconf goes here ... - ) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) .. note:: @@ -124,9 +124,9 @@ this by adding the following snippet to your urls.py:: from django.conf import settings from django.conf.urls.static import static - urlpatterns = patterns('', + urlpatterns = [ # ... the rest of your URLconf goes here ... - ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) .. note:: diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index e06765742e..dc51a5c9cc 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -15,6 +15,11 @@ about each item can often be found in the release notes of two versions prior. * ``cycle`` and ``firstof`` template tags will be removed from the ``future`` template tag library (used during the 1.6/1.7 deprecation period). +* ``django.conf.urls.patterns()`` will be removed. + +* Support for the ``prefix`` argument to + ``django.conf.urls.i18n.i18n_patterns()`` will be removed. + .. _deprecation-removed-in-1.9: 1.9 diff --git a/docs/intro/overview.txt b/docs/intro/overview.txt index 6ec3baf0ec..70c4663153 100644 --- a/docs/intro/overview.txt +++ b/docs/intro/overview.txt @@ -184,13 +184,13 @@ to decouple URLs from Python code. Here's what a URLconf might look like for the ``Reporter``/``Article`` example above:: - from django.conf.urls import patterns + from django.conf.urls import url - urlpatterns = patterns('', - (r'^articles/(\d{4})/$', 'news.views.year_archive'), - (r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'), - (r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'), - ) + urlpatterns = [ + url(r'^articles/(\d{4})/$', 'news.views.year_archive'), + url(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'), + url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'), + ] The code above maps URLs, as simple `regular expressions`_, to the location of Python callback functions ("views"). The regular expressions use parenthesis to diff --git a/docs/intro/tutorial03.txt b/docs/intro/tutorial03.txt index 5b2bee5fe7..3d4f65a720 100644 --- a/docs/intro/tutorial03.txt +++ b/docs/intro/tutorial03.txt @@ -92,13 +92,13 @@ In the ``polls/urls.py`` file include the following code: .. snippet:: :filename: polls/urls.py - from django.conf.urls import patterns, url + from django.conf.urls import url from polls import views - urlpatterns = patterns('', + urlpatterns = [ url(r'^$', views.index, name='index') - ) + ] The next step is to point the root URLconf at the ``polls.urls`` module. In ``mysite/urls.py`` insert an :func:`~django.conf.urls.include`, leaving you @@ -107,13 +107,13 @@ with: .. snippet:: :filename: mysite/urls.py - from django.conf.urls import patterns, include, url + from django.conf.urls import include, url from django.contrib import admin - urlpatterns = patterns('', + urlpatterns = [ url(r'^polls/', include('polls.urls')), url(r'^admin/', include(admin.site.urls)), - ) + ] .. admonition:: Doesn't match what you see? @@ -207,11 +207,11 @@ Wire these new views into the ``polls.urls`` module by adding the following .. snippet:: :filename: polls/urls.py - from django.conf.urls import patterns, url + from django.conf.urls import url from polls import views - urlpatterns = patterns('', + urlpatterns = [ # ex: /polls/ url(r'^$', views.index, name='index'), # ex: /polls/5/ @@ -220,7 +220,7 @@ Wire these new views into the ``polls.urls`` module by adding the following url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'), # ex: /polls/5/vote/ url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'), - ) + ] Take a look in your browser, at "/polls/34/". It'll run the ``detail()`` method and display whatever ID you provide in the URL. Try @@ -583,13 +583,13 @@ it to include namespacing: .. snippet:: :filename: mysite/urls.py - from django.conf.urls import patterns, include, url + from django.conf.urls import include, url from django.contrib import admin - urlpatterns = patterns('', + urlpatterns = [ url(r'^polls/', include('polls.urls', namespace="polls")), url(r'^admin/', include(admin.site.urls)), - ) + ] Now change your ``polls/index.html`` template from: diff --git a/docs/intro/tutorial04.txt b/docs/intro/tutorial04.txt index fbccb93a70..63d12d1a68 100644 --- a/docs/intro/tutorial04.txt +++ b/docs/intro/tutorial04.txt @@ -222,16 +222,16 @@ First, open the ``polls/urls.py`` URLconf and change it like so: .. snippet:: :filename: polls/urls.py - from django.conf.urls import patterns, url + from django.conf.urls import url from polls import views - urlpatterns = patterns('', + urlpatterns = [ url(r'^$', views.IndexView.as_view(), name='index'), url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'), url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'), url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'), - ) + ] Amend views ----------- diff --git a/docs/ref/class-based-views/base.txt b/docs/ref/class-based-views/base.txt index fb98f9d80e..e8f5271e97 100644 --- a/docs/ref/class-based-views/base.txt +++ b/docs/ref/class-based-views/base.txt @@ -39,13 +39,13 @@ View **Example urls.py**:: - from django.conf.urls import patterns, url + from django.conf.urls import url from myapp.views import MyView - urlpatterns = patterns('', + urlpatterns = [ url(r'^mine/$', MyView.as_view(), name='my-view'), - ) + ] **Attributes** @@ -131,13 +131,13 @@ TemplateView **Example urls.py**:: - from django.conf.urls import patterns, url + from django.conf.urls import url from myapp.views import HomePageView - urlpatterns = patterns('', + urlpatterns = [ url(r'^$', HomePageView.as_view(), name='home'), - ) + ] **Context** @@ -192,17 +192,16 @@ RedirectView **Example urls.py**:: - from django.conf.urls import patterns, url + from django.conf.urls import url from django.views.generic.base import RedirectView from article.views import ArticleCounterRedirectView, ArticleDetail - urlpatterns = patterns('', - + urlpatterns = [ url(r'^counter/(?P<pk>\d+)/$', ArticleCounterRedirectView.as_view(), name='article-counter'), url(r'^details/(?P<pk>\d+)/$', ArticleDetail.as_view(), name='article-detail'), url(r'^go-to-django/$', RedirectView.as_view(url='http://djangoproject.com'), name='go-to-django'), - ) + ] **Attributes** diff --git a/docs/ref/class-based-views/generic-date-based.txt b/docs/ref/class-based-views/generic-date-based.txt index bf70f2313b..9d6d447052 100644 --- a/docs/ref/class-based-views/generic-date-based.txt +++ b/docs/ref/class-based-views/generic-date-based.txt @@ -65,16 +65,16 @@ ArchiveIndexView **Example myapp/views.py**:: - from django.conf.urls import patterns, url + from django.conf.urls import url from django.views.generic.dates import ArchiveIndexView from myapp.models import Article - urlpatterns = patterns('', + urlpatterns = [ url(r'^archive/$', ArchiveIndexView.as_view(model=Article, date_field="pub_date"), name="article_archive"), - ) + ] **Example myapp/article_archive.html**: @@ -166,15 +166,15 @@ YearArchiveView **Example myapp/urls.py**:: - from django.conf.urls import patterns, url + from django.conf.urls import url from myapp.views import ArticleYearArchiveView - urlpatterns = patterns('', + urlpatterns = [ url(r'^(?P<year>\d{4})/$', ArticleYearArchiveView.as_view(), name="article_year_archive"), - ) + ] **Example myapp/article_archive_year.html**: @@ -261,11 +261,11 @@ MonthArchiveView **Example myapp/urls.py**:: - from django.conf.urls import patterns, url + from django.conf.urls import url from myapp.views import ArticleMonthArchiveView - urlpatterns = patterns('', + urlpatterns = [ # Example: /2012/aug/ url(r'^(?P<year>\d{4})/(?P<month>[-\w]+)/$', ArticleMonthArchiveView.as_view(), @@ -274,7 +274,7 @@ MonthArchiveView url(r'^(?P<year>\d{4})/(?P<month>\d+)/$', ArticleMonthArchiveView.as_view(month_format='%m'), name="archive_month_numeric"), - ) + ] **Example myapp/article_archive_month.html**: @@ -355,16 +355,16 @@ WeekArchiveView **Example myapp/urls.py**:: - from django.conf.urls import patterns, url + from django.conf.urls import url from myapp.views import ArticleWeekArchiveView - urlpatterns = patterns('', + urlpatterns = [ # Example: /2012/week/23/ url(r'^(?P<year>\d{4})/week/(?P<week>\d+)/$', ArticleWeekArchiveView.as_view(), name="archive_week"), - ) + ] **Example myapp/article_archive_week.html**: @@ -469,16 +469,16 @@ DayArchiveView **Example myapp/urls.py**:: - from django.conf.urls import patterns, url + from django.conf.urls import url from myapp.views import ArticleDayArchiveView - urlpatterns = patterns('', + urlpatterns = [ # Example: /2012/nov/10/ url(r'^(?P<year>\d{4})/(?P<month>[-\w]+)/(?P<day>\d+)/$', ArticleDayArchiveView.as_view(), name="archive_day"), - ) + ] **Example myapp/article_archive_day.html**: @@ -543,15 +543,15 @@ TodayArchiveView **Example myapp/urls.py**:: - from django.conf.urls import patterns, url + from django.conf.urls import url from myapp.views import ArticleTodayArchiveView - urlpatterns = patterns('', + urlpatterns = [ url(r'^today/$', ArticleTodayArchiveView.as_view(), name="archive_today"), - ) + ] .. admonition:: Where is the example template for ``TodayArchiveView``? @@ -593,14 +593,14 @@ DateDetailView **Example myapp/urls.py**:: - from django.conf.urls import patterns, url + from django.conf.urls import url from django.views.generic.dates import DateDetailView - urlpatterns = patterns('', + urlpatterns = [ url(r'^(?P<year>\d+)/(?P<month>[-\w]+)/(?P<day>\d+)/(?P<pk>\d+)/$', DateDetailView.as_view(model=Article, date_field="pub_date"), name="archive_date_detail"), - ) + ] **Example myapp/article_detail.html**: diff --git a/docs/ref/class-based-views/generic-display.txt b/docs/ref/class-based-views/generic-display.txt index 24006d48d3..1ca8125501 100644 --- a/docs/ref/class-based-views/generic-display.txt +++ b/docs/ref/class-based-views/generic-display.txt @@ -54,13 +54,13 @@ DetailView **Example myapp/urls.py**:: - from django.conf.urls import patterns, url + from django.conf.urls import url from article.views import ArticleDetailView - urlpatterns = patterns('', + urlpatterns = [ url(r'^(?P<slug>[-_\w]+)/$', ArticleDetailView.as_view(), name='article-detail'), - ) + ] **Example myapp/article_detail.html**: @@ -123,13 +123,13 @@ ListView **Example myapp/urls.py**:: - from django.conf.urls import patterns, url + from django.conf.urls import url from article.views import ArticleListView - urlpatterns = patterns('', + urlpatterns = [ url(r'^$', ArticleListView.as_view(), name='article-list'), - ) + ] **Example myapp/article_list.html**: diff --git a/docs/ref/class-based-views/index.txt b/docs/ref/class-based-views/index.txt index 821edc0874..81d5c560f9 100644 --- a/docs/ref/class-based-views/index.txt +++ b/docs/ref/class-based-views/index.txt @@ -25,9 +25,9 @@ a thread-safe operation). A class-based view is deployed into a URL pattern using the :meth:`~django.views.generic.base.View.as_view()` classmethod:: - urlpatterns = patterns('', - (r'^view/$', MyView.as_view(size=42)), - ) + urlpatterns = [ + url(r'^view/$', MyView.as_view(size=42)), + ] .. admonition:: Thread safety with view arguments diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 7ebe90060b..d999b11efe 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -1395,9 +1395,9 @@ templates used by the :class:`ModelAdmin` views: class MyModelAdmin(admin.ModelAdmin): def get_urls(self): urls = super(MyModelAdmin, self).get_urls() - my_urls = patterns('', - (r'^my_view/$', self.my_view) - ) + my_urls = [ + url(r'^my_view/$', self.my_view), + ] return my_urls + urls def my_view(self, request): @@ -1432,9 +1432,9 @@ templates used by the :class:`ModelAdmin` views: class MyModelAdmin(admin.ModelAdmin): def get_urls(self): urls = super(MyModelAdmin, self).get_urls() - my_urls = patterns('', - (r'^my_view/$', self.admin_site.admin_view(self.my_view)) - ) + my_urls = [ + url(r'^my_view/$', self.admin_site.admin_view(self.my_view)) + ] return my_urls + urls Notice the wrapped view in the fifth line above:: @@ -2434,23 +2434,23 @@ In this example, we register the default ``AdminSite`` instance ``django.contrib.admin.site`` at the URL ``/admin/`` :: # urls.py - from django.conf.urls import patterns, include + from django.conf.urls import include, url from django.contrib import admin - urlpatterns = patterns('', - (r'^admin/', include(admin.site.urls)), - ) + urlpatterns = [ + url(r'^admin/', include(admin.site.urls)), + ] In this example, we register the ``AdminSite`` instance ``myproject.admin.admin_site`` at the URL ``/myadmin/`` :: # urls.py - from django.conf.urls import patterns, include + from django.conf.urls import include, url from myproject.admin import admin_site - urlpatterns = patterns('', - (r'^myadmin/', include(admin_site.urls)), - ) + urlpatterns = [ + url(r'^myadmin/', include(admin_site.urls)), + ] Note that you may not want autodiscovery of ``admin`` modules when using your own ``AdminSite`` instance since you will likely be importing all the per-app @@ -2472,13 +2472,13 @@ separate versions of the admin site -- using the ``AdminSite`` instances respectively:: # urls.py - from django.conf.urls import patterns, include + from django.conf.urls import include, url from myproject.admin import basic_site, advanced_site - urlpatterns = patterns('', - (r'^basic-admin/', include(basic_site.urls)), - (r'^advanced-admin/', include(advanced_site.urls)), - ) + urlpatterns = [ + url(r'^basic-admin/', include(basic_site.urls)), + url(r'^advanced-admin/', include(advanced_site.urls)), + ] ``AdminSite`` instances take a single argument to their constructor, their name, which can be anything you like. This argument becomes the prefix to the diff --git a/docs/ref/contrib/flatpages.txt b/docs/ref/contrib/flatpages.txt index be9fe0c636..b280e0ddc4 100644 --- a/docs/ref/contrib/flatpages.txt +++ b/docs/ref/contrib/flatpages.txt @@ -46,9 +46,9 @@ Then either: 3. Add an entry in your URLconf. For example:: - urlpatterns = patterns('', - (r'^pages/', include('django.contrib.flatpages.urls')), - ) + urlpatterns = [ + url(r'^pages/', include('django.contrib.flatpages.urls')), + ] or: @@ -73,17 +73,19 @@ Using the URLconf There are several ways to include the flat pages in your URLconf. You can dedicate a particular path to flat pages:: - urlpatterns = patterns('', - (r'^pages/', include('django.contrib.flatpages.urls')), - ) + urlpatterns = [ + url(r'^pages/', include('django.contrib.flatpages.urls')), + ] You can also set it up as a "catchall" pattern. In this case, it is important to place the pattern at the end of the other urlpatterns:: + from django.contrib.flatpages import views + # Your other patterns here - urlpatterns += patterns('django.contrib.flatpages.views', - (r'^(?P<url>.*/)$', 'flatpage'), - ) + urlpatterns += [ + url(r'^(?P<url>.*/)$', views.flatpage), + ] .. warning:: @@ -95,10 +97,12 @@ Another common setup is to use flat pages for a limited set of known pages and to hard code the urls, so you can reference them with the :ttag:`url` template tag:: - urlpatterns += patterns('django.contrib.flatpages.views', - url(r'^about-us/$', 'flatpage', {'url': '/about-us/'}, name='about'), - url(r'^license/$', 'flatpage', {'url': '/license/'}, name='license'), - ) + from django.contrib.flatpages import views + + urlpatterns += [ + url(r'^about-us/$', views.flatpage, {'url': '/about-us/'}, name='about'), + url(r'^license/$', views.flatpage, {'url': '/license/'}, name='license'), + ] Using the middleware -------------------- diff --git a/docs/ref/contrib/formtools/form-wizard.txt b/docs/ref/contrib/formtools/form-wizard.txt index 2934893776..054c027ea8 100644 --- a/docs/ref/contrib/formtools/form-wizard.txt +++ b/docs/ref/contrib/formtools/form-wizard.txt @@ -251,14 +251,14 @@ deploy the new :class:`WizardView` object at a URL in the ``urls.py``. The wizard's ``as_view()`` method takes a list of your :class:`~django.forms.Form` classes as an argument during instantiation:: - from django.conf.urls import patterns + from django.conf.urls import url from myapp.forms import ContactForm1, ContactForm2 from myapp.views import ContactWizard - urlpatterns = patterns('', - (r'^contact/$', ContactWizard.as_view([ContactForm1, ContactForm2])), - ) + urlpatterns = [ + url(r'^contact/$', ContactWizard.as_view([ContactForm1, ContactForm2])), + ] You can also pass the form list as a class attribute named ``form_list``:: @@ -311,9 +311,9 @@ Here's what the view code might look like:: The ``urls.py`` file would contain something like:: - urlpatterns = patterns('', - (r'^checkout/$', OrderWizard.as_view(FORMS, condition_dict={'cc': pay_by_credit_card})), - ) + urlpatterns = [ + url(r'^checkout/$', OrderWizard.as_view(FORMS, condition_dict={'cc': pay_by_credit_card})), + ] The ``condition_dict`` can be passed as attribute for the ``as_view()` method or as a class attribute named ``condition_dict``:: @@ -673,18 +673,18 @@ We define our wizard in a ``views.py``:: We need to add the ``ContactWizard`` to our ``urls.py`` file:: - from django.conf.urls import patterns + from django.conf.urls import url from myapp.forms import ContactForm1, ContactForm2 from myapp.views import ContactWizard, show_message_form_condition contact_forms = [ContactForm1, ContactForm2] - urlpatterns = patterns('', - (r'^contact/$', ContactWizard.as_view(contact_forms, + urlpatterns = [ + url(r'^contact/$', ContactWizard.as_view(contact_forms, condition_dict={'1': show_message_form_condition} )), - ) + ] As you can see, we defined a ``show_message_form_condition`` next to our :class:`WizardView` subclass and added a ``condition_dict`` argument to the @@ -728,7 +728,7 @@ Additionally you have to pass two more arguments to the Example code for the changed ``urls.py`` file:: - from django.conf.urls import url, patterns + from django.conf.urls import url from myapp.forms import ContactForm1, ContactForm2 from myapp.views import ContactWizard @@ -741,10 +741,10 @@ Example code for the changed ``urls.py`` file:: contact_wizard = ContactWizard.as_view(named_contact_forms, url_name='contact_step', done_step_name='finished') - urlpatterns = patterns('', + urlpatterns = [ url(r'^contact/(?P<step>.+)/$', contact_wizard, name='contact_step'), url(r'^contact/$', contact_wizard, name='contact'), - ) + ] Advanced ``NamedUrlWizardView`` methods ======================================= diff --git a/docs/ref/contrib/gis/tutorial.txt b/docs/ref/contrib/gis/tutorial.txt index 24470e8227..fee293728e 100644 --- a/docs/ref/contrib/gis/tutorial.txt +++ b/docs/ref/contrib/gis/tutorial.txt @@ -730,12 +730,12 @@ Let's dive right in. Create a file called ``admin.py`` inside the Next, edit your ``urls.py`` in the ``geodjango`` application folder as follows:: - from django.conf.urls import patterns, url, include + from django.conf.urls import url, include from django.contrib.gis import admin - urlpatterns = patterns('', + urlpatterns = [ url(r'^admin/', include(admin.site.urls)), - ) + ] Start up the Django development server: diff --git a/docs/ref/contrib/sitemaps.txt b/docs/ref/contrib/sitemaps.txt index fefe90a420..12ed61b97a 100644 --- a/docs/ref/contrib/sitemaps.txt +++ b/docs/ref/contrib/sitemaps.txt @@ -265,7 +265,7 @@ Example Here's an example of a :doc:`URLconf </topics/http/urls>` using both:: - from django.conf.urls import patterns + from django.conf.urls import url from django.contrib.sitemaps import FlatPageSitemap, GenericSitemap from blog.models import Entry @@ -279,13 +279,13 @@ Here's an example of a :doc:`URLconf </topics/http/urls>` using both:: 'blog': GenericSitemap(info_dict, priority=0.6), } - urlpatterns = patterns('', + urlpatterns = [ # some generic view using info_dict # ... # the sitemap - (r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}) - ) + url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}), + ] .. _URLconf: ../url_dispatch/ @@ -313,20 +313,20 @@ the sitemap. For example:: return reverse(item) # urls.py - from django.conf.urls import patterns, url + from django.conf.urls import url from .sitemaps import StaticViewSitemap sitemaps = { 'static': StaticViewSitemap, } - urlpatterns = patterns('', + urlpatterns = [ url(r'^$', 'views.main', name='main'), url(r'^about/$', 'views.about', name='about'), url(r'^license/$', 'views.license', name='license'), # ... url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}) - ) + ] Creating a sitemap index @@ -345,10 +345,12 @@ references individual sitemap files, one per each section defined in your Here's what the relevant URLconf lines would look like for the example above:: - urlpatterns = patterns('django.contrib.sitemaps.views', - (r'^sitemap\.xml$', 'index', {'sitemaps': sitemaps}), - (r'^sitemap-(?P<section>.+)\.xml$', 'sitemap', {'sitemaps': sitemaps}), - ) + from django.contrib.sitemaps import views + + urlpatterns = [ + url(r'^sitemap\.xml$', views.index, {'sitemaps': sitemaps}), + url(r'^sitemap-(?P<section>.+)\.xml$', views.sitemap, {'sitemaps': sitemaps}), + ] This will automatically generate a :file:`sitemap.xml` file that references both :file:`sitemap-flatpages.xml` and :file:`sitemap-blog.xml`. The @@ -366,14 +368,14 @@ with a caching decorator -- you must name your sitemap view and pass from django.contrib.sitemaps import views as sitemaps_views from django.views.decorators.cache import cache_page - urlpatterns = patterns('', + urlpatterns = [ url(r'^sitemap\.xml$', cache_page(86400)(sitemaps_views.index), {'sitemaps': sitemaps, 'sitemap_url_name': 'sitemaps'}), url(r'^sitemap-(?P<section>.+)\.xml$', cache_page(86400)(sitemaps_views.sitemap), {'sitemaps': sitemaps}, name='sitemaps'), - ) + ] Template customization @@ -383,16 +385,18 @@ If you wish to use a different template for each sitemap or sitemap index available on your site, you may specify it by passing a ``template_name`` parameter to the ``sitemap`` and ``index`` views via the URLconf:: - urlpatterns = patterns('django.contrib.sitemaps.views', - (r'^custom-sitemap\.xml$', 'index', { + from django.contrib.sitemaps import views + + urlpatterns = [ + url(r'^custom-sitemap\.xml$', views.index, { 'sitemaps': sitemaps, 'template_name': 'custom_sitemap.html' }), - (r'^custom-sitemap-(?P<section>.+)\.xml$', 'sitemap', { + url(r'^custom-sitemap-(?P<section>.+)\.xml$', views.sitemap, { 'sitemaps': sitemaps, 'template_name': 'custom_sitemap.html' }), - ) + ] These views return :class:`~django.template.response.TemplateResponse` diff --git a/docs/ref/contrib/staticfiles.txt b/docs/ref/contrib/staticfiles.txt index 961abf5c5d..8f7ba0a6fe 100644 --- a/docs/ref/contrib/staticfiles.txt +++ b/docs/ref/contrib/staticfiles.txt @@ -445,11 +445,12 @@ local development server, add the following snippet to the end of your primary URL configuration:: from django.conf import settings + from django.contrib.staticfiles import views if settings.DEBUG: - urlpatterns += patterns('django.contrib.staticfiles.views', - url(r'^static/(?P<path>.*)$', 'serve'), - ) + urlpatterns += [ + url(r'^static/(?P<path>.*)$', views.serve), + ] Note, the beginning of the pattern (``r'^static/'``) should be your :setting:`STATIC_URL` setting. diff --git a/docs/ref/contrib/syndication.txt b/docs/ref/contrib/syndication.txt index 61adfef35d..b2ebf40e61 100644 --- a/docs/ref/contrib/syndication.txt +++ b/docs/ref/contrib/syndication.txt @@ -77,14 +77,14 @@ a feed of the latest five news items:: To connect a URL to this feed, put an instance of the Feed object in your :doc:`URLconf </topics/http/urls>`. For example:: - from django.conf.urls import patterns + from django.conf.urls import url from myproject.feeds import LatestEntriesFeed - urlpatterns = patterns('', + urlpatterns = [ # ... - (r'^latest/feed/$', LatestEntriesFeed()), + url(r'^latest/feed/$', LatestEntriesFeed()), # ... - ) + ] Note: @@ -366,15 +366,15 @@ Here's a full example:: And the accompanying URLconf:: - from django.conf.urls import patterns + from django.conf.urls import url from myproject.feeds import RssSiteNewsFeed, AtomSiteNewsFeed - urlpatterns = patterns('', + urlpatterns = [ # ... - (r'^sitenews/rss/$', RssSiteNewsFeed()), - (r'^sitenews/atom/$', AtomSiteNewsFeed()), + url(r'^sitenews/rss/$', RssSiteNewsFeed()), + url(r'^sitenews/atom/$', AtomSiteNewsFeed()), # ... - ) + ] Feed class reference -------------------- diff --git a/docs/ref/urls.txt b/docs/ref/urls.txt index 1806e1980f..47ef488afc 100644 --- a/docs/ref/urls.txt +++ b/docs/ref/urls.txt @@ -9,11 +9,42 @@ patterns() .. function:: patterns(prefix, pattern_description, ...) +.. deprecated:: 1.8 + + ``urlpatterns`` should be a plain list of :func:`django.conf.urls.url` + instances instead. + A function that takes a prefix, and an arbitrary number of URL patterns, and returns a list of URL patterns in the format Django needs. -The first argument to ``patterns()`` is a string ``prefix``. See -:ref:`The view prefix <urlpatterns-view-prefix>`. +The first argument to ``patterns()`` is a string ``prefix``. Here's the example +URLconf from the :doc:`Django overview </intro/overview>`:: + + from django.conf.urls import patterns, url + + urlpatterns = patterns('', + url(r'^articles/(\d{4})/$', 'news.views.year_archive'), + url(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'), + url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'), + ) + +In this example, each view has a common prefix -- ``'news.views'``. +Instead of typing that out for each entry in ``urlpatterns``, you can use the +first argument to the ``patterns()`` function to specify a prefix to apply to +each view function. + +With this in mind, the above example can be written more concisely as:: + + from django.conf.urls import patterns, url + + urlpatterns = patterns('news.views', + url(r'^articles/(\d{4})/$', 'year_archive'), + url(r'^articles/(\d{4})/(\d{2})/$', 'month_archive'), + url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'article_detail'), + ) + +Note that you don't put a trailing dot (``"."``) in the prefix. Django puts +that in automatically. The remaining arguments should be tuples in this format:: @@ -54,23 +85,21 @@ Helper function to return a URL pattern for serving files in debug mode:: from django.conf import settings from django.conf.urls.static import static - urlpatterns = patterns('', + urlpatterns = [ # ... the rest of your URLconf goes here ... - ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) url() ----- .. function:: url(regex, view, kwargs=None, name=None, prefix='') -You can use the ``url()`` function, instead of a tuple, as an argument to -``patterns()``. This is convenient if you want to specify a name without the -optional extra arguments dictionary. For example:: +``urlpatterns`` should be a list of ``url()`` instances. For example:: - urlpatterns = patterns('', + urlpatterns = [ url(r'^index/$', index_view, name="main-view"), ... - ) + ] This function takes five arguments, most of which are optional:: @@ -107,7 +136,7 @@ include() :type namespace: string :arg app_name: Application namespace for the URL entries being included :type app_name: string - :arg pattern_list: Iterable of URL entries as returned by :func:`patterns` + :arg pattern_list: Iterable of :func:`django.conf.urls.url` instances :arg app_namespace: Application namespace for the URL entries being included :type app_namespace: string :arg instance_namespace: Instance namespace for the URL entries being included diff --git a/docs/ref/views.txt b/docs/ref/views.txt index 8c9c3e3ed8..c5d36abe0a 100644 --- a/docs/ref/views.txt +++ b/docs/ref/views.txt @@ -26,15 +26,16 @@ built-in handling for user-uploaded files, but you can have Django serve your :setting:`MEDIA_ROOT` by appending something like this to your URLconf:: from django.conf import settings + from django.views.static import serve # ... the rest of your URLconf goes here ... if settings.DEBUG: - urlpatterns += patterns('', - url(r'^media/(?P<path>.*)$', 'django.views.static.serve', { + urlpatterns += [ + url(r'^media/(?P<path>.*)$', serve, { 'document_root': settings.MEDIA_ROOT, }), - ) + ] Note, the snippet assumes your :setting:`MEDIA_URL` has a value of ``'/media/'``. This will call the :func:`~django.views.static.serve` view, diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index d86836a50f..6112ea4744 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -203,3 +203,62 @@ Django 1.6 introduced ``{% load cycle from future %}`` and :ttag:`cycle` and :ttag:`firstof` template tags. This syntax is now deprecated and will be removed in Django 2.0. You can simply remove the ``{% load ... from future %}`` tags. + +``django.conf.urls.patterns()`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +In the olden days of Django, it was encouraged to reference views as strings +in ``urlpatterns``:: + + 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`` argument which is prepended to all +views-as-strings in that set of ``urlpatterns``:: + + urlpatterns = patterns('myapp.views', + url('^$', 'myview'), + url('^other/$', 'otherview'), + ) + +In the modern era, we have updated the tutorial to instead recommend 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 using +normal Python in place of "Django String Magic": the errors when you mistype a +view name are less obscure, IDEs can help with autocompletion of view names, +etc. + +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), + ) + +Thus ``patterns()`` serves little purpose and is a burden when teaching new users +(answering the newbie's question "why do I need this empty string as the first +argument to ``patterns()``?"). For these reasons, we are deprecating it. +Updating your code is as simple as ensuring that ``urlpatterns`` is a string of +:func:`django.conf.urls.url` instances. For example:: + + from django.conf.urls import url + from myapp import views + + urlpatterns = [ + url('^$', views.myview), + url('^other/$', views.otherview), + ] + +``prefix`` argument to :func:`~django.conf.urls.i18n.i18n_patterns` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Related to the previous item, the ``prefix`` argument to +:func:`django.conf.urls.i18n.i18n_patterns` has been deprecated. Simply pass a +list of :func:`django.conf.urls.url` instances instead. diff --git a/docs/spelling_wordlist b/docs/spelling_wordlist index 78626ae2de..df695587e2 100644 --- a/docs/spelling_wordlist +++ b/docs/spelling_wordlist @@ -23,6 +23,7 @@ attr auth autoclobber autocommit +autocompletion autoconf autodetect autodetectable diff --git a/docs/topics/cache.txt b/docs/topics/cache.txt index f9623b324b..feeaf37a47 100644 --- a/docs/topics/cache.txt +++ b/docs/topics/cache.txt @@ -532,9 +532,9 @@ The per-view cache, like the per-site cache, is keyed off of the URL. If multiple URLs point at the same view, each URL will be cached separately. Continuing the ``my_view`` example, if your URLconf looks like this:: - urlpatterns = ('', - (r'^foo/(\d{1,2})/$', my_view), - ) + urlpatterns = [ + url(r'^foo/(\d{1,2})/$', my_view), + ] then requests to ``/foo/1/`` and ``/foo/23/`` will be cached separately, as you may expect. But once a particular URL (e.g., ``/foo/23/``) has been @@ -578,17 +578,17 @@ themselves. Doing so is easy: simply wrap the view function with ``cache_page`` when you refer to it in the URLconf. Here's the old URLconf from earlier:: - urlpatterns = ('', - (r'^foo/(\d{1,2})/$', my_view), - ) + urlpatterns = [ + url(r'^foo/(\d{1,2})/$', my_view), + ] Here's the same thing, with ``my_view`` wrapped in ``cache_page``:: from django.views.decorators.cache import cache_page - urlpatterns = ('', - (r'^foo/(\d{1,2})/$', cache_page(60 * 15)(my_view)), - ) + urlpatterns = [ + url(r'^foo/(\d{1,2})/$', cache_page(60 * 15)(my_view)), + ] .. templatetag:: cache diff --git a/docs/topics/class-based-views/generic-display.txt b/docs/topics/class-based-views/generic-display.txt index 9c5527dac5..98d2b79d32 100644 --- a/docs/topics/class-based-views/generic-display.txt +++ b/docs/topics/class-based-views/generic-display.txt @@ -119,12 +119,12 @@ Now we need to define a view:: Finally hook that view into your urls:: # urls.py - from django.conf.urls import patterns, url + from django.conf.urls import url from books.views import PublisherList - urlpatterns = patterns('', + urlpatterns = [ url(r'^publishers/$', PublisherList.as_view()), - ) + ] That's all the Python code we need to write. We still need to write a template, however. We could explicitly tell the view which template to use by adding a @@ -330,12 +330,12 @@ various useful things are stored on ``self``; as well as the request Here, we have a URLconf with a single captured group:: # urls.py - from django.conf.urls import patterns + from django.conf.urls import url from books.views import PublisherBookList - urlpatterns = patterns('', - (r'^books/([\w-]+)/$', PublisherBookList.as_view()), - ) + urlpatterns = [ + url(r'^books/([\w-]+)/$', PublisherBookList.as_view()), + ] Next, we'll write the ``PublisherBookList`` view itself:: @@ -396,13 +396,13 @@ updated. First, we'd need to add an author detail bit in the URLconf to point to a custom view:: - from django.conf.urls import patterns, url + from django.conf.urls import url from books.views import AuthorDetailView - urlpatterns = patterns('', + urlpatterns = [ #... url(r'^authors/(?P<pk>\d+)/$', AuthorDetailView.as_view(), name='author-detail'), - ) + ] Then we'd write our new view -- ``get_object`` is the method that retrieves the object -- so we simply override it and wrap the call:: diff --git a/docs/topics/class-based-views/generic-editing.txt b/docs/topics/class-based-views/generic-editing.txt index 44680148ab..2587c9426d 100644 --- a/docs/topics/class-based-views/generic-editing.txt +++ b/docs/topics/class-based-views/generic-editing.txt @@ -141,15 +141,15 @@ an :exc:`~django.core.exceptions.ImproperlyConfigured` exception if it's not. Finally, we hook these new views into the URLconf:: # urls.py - from django.conf.urls import patterns, url + from django.conf.urls import url from myapp.views import AuthorCreate, AuthorUpdate, AuthorDelete - urlpatterns = patterns('', + urlpatterns = [ # ... url(r'author/add/$', AuthorCreate.as_view(), name='author_add'), url(r'author/(?P<pk>\d+)/$', AuthorUpdate.as_view(), name='author_update'), url(r'author/(?P<pk>\d+)/delete/$', AuthorDelete.as_view(), name='author_delete'), - ) + ] .. note:: diff --git a/docs/topics/class-based-views/index.txt b/docs/topics/class-based-views/index.txt index 84e4597842..dc77f04721 100644 --- a/docs/topics/class-based-views/index.txt +++ b/docs/topics/class-based-views/index.txt @@ -38,12 +38,12 @@ URLconf. If you're only changing a few simple attributes on a class-based view, you can simply pass them into the :meth:`~django.views.generic.base.View.as_view` method call itself:: - from django.conf.urls import patterns + from django.conf.urls import url from django.views.generic import TemplateView - urlpatterns = patterns('', - (r'^about/', TemplateView.as_view(template_name="about.html")), - ) + urlpatterns = [ + url(r'^about/', TemplateView.as_view(template_name="about.html")), + ] Any arguments passed to :meth:`~django.views.generic.base.View.as_view` will override attributes set on the class. In this example, we set ``template_name`` @@ -75,12 +75,12 @@ class method instead, which provides a function-like entry to class-based views:: # urls.py - from django.conf.urls import patterns + from django.conf.urls import url from some_app.views import AboutView - urlpatterns = patterns('', - (r'^about/', AboutView.as_view()), - ) + urlpatterns = [ + url(r'^about/', AboutView.as_view()), + ] For more information on how to use the built in generic views, consult the next @@ -100,12 +100,12 @@ preferable to ask the API when the most recent book was published. We map the URL to book list view in the URLconf:: - from django.conf.urls import patterns + from django.conf.urls import url from books.views import BookListView - urlpatterns = patterns('', - (r'^books/$', BookListView.as_view()), - ) + urlpatterns = [ + url(r'^books/$', BookListView.as_view()), + ] And the view:: diff --git a/docs/topics/class-based-views/intro.txt b/docs/topics/class-based-views/intro.txt index dbd7a83a31..53f32198c6 100644 --- a/docs/topics/class-based-views/intro.txt +++ b/docs/topics/class-based-views/intro.txt @@ -89,12 +89,12 @@ request to a matching method if one is defined, or raises :class:`~django.http.HttpResponseNotAllowed` if not:: # urls.py - from django.conf.urls import patterns + from django.conf.urls import url from myapp.views import MyView - urlpatterns = patterns('', - (r'^about/', MyView.as_view()), - ) + urlpatterns = [ + url(r'^about/', MyView.as_view()), + ] It is worth noting that what your method returns is identical to what you @@ -129,9 +129,9 @@ You can override that in a subclass:: Another option is to configure class attributes as keyword arguments to the :meth:`~django.views.generic.base.View.as_view` call in the URLconf:: - urlpatterns = patterns('', - (r'^about/', GreetingView.as_view(greeting="G'day")), - ) + urlpatterns = [ + url(r'^about/', GreetingView.as_view(greeting="G'day")), + ] .. note:: @@ -268,10 +268,10 @@ The easiest place to do this is in the URLconf where you deploy your view:: from .views import VoteView - urlpatterns = patterns('', - (r'^about/', login_required(TemplateView.as_view(template_name="secret.html"))), - (r'^vote/', permission_required('polls.can_vote')(VoteView.as_view())), - ) + urlpatterns = [ + url(r'^about/', login_required(TemplateView.as_view(template_name="secret.html"))), + url(r'^vote/', permission_required('polls.can_vote')(VoteView.as_view())), + ] This approach applies the decorator on a per-instance basis. If you want every instance of a view to be decorated, you need to take a diff --git a/docs/topics/class-based-views/mixins.txt b/docs/topics/class-based-views/mixins.txt index f0bacca6c1..00729f36f9 100644 --- a/docs/topics/class-based-views/mixins.txt +++ b/docs/topics/class-based-views/mixins.txt @@ -256,13 +256,13 @@ mixin. We can hook this into our URLs easily enough:: # urls.py - from django.conf.urls import patterns, url + from django.conf.urls import url from books.views import RecordInterest - urlpatterns = patterns('', + urlpatterns = [ #... url(r'^author/(?P<pk>\d+)/interest/$', RecordInterest.as_view(), name='author-interest'), - ) + ] Note the ``pk`` named group, which :meth:`~django.views.generic.detail.SingleObjectMixin.get_object` uses diff --git a/docs/topics/http/urls.txt b/docs/topics/http/urls.txt index 46086bc420..05525cc715 100644 --- a/docs/topics/http/urls.txt +++ b/docs/topics/http/urls.txt @@ -45,8 +45,8 @@ algorithm the system follows to determine which Python code to execute: will be used in place of the :setting:`ROOT_URLCONF` setting. 2. Django loads that Python module and looks for the variable - ``urlpatterns``. This should be a Python list, in the format returned by - the function :func:`django.conf.urls.patterns`. + ``urlpatterns``. This should be a Python list of :func:`django.conf.urls.url` + instances. 3. Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL. @@ -72,14 +72,14 @@ Example Here's a sample URLconf:: - from django.conf.urls import patterns, url + from django.conf.urls import url - urlpatterns = patterns('', + urlpatterns = [ url(r'^articles/2003/$', 'news.views.special_case_2003'), url(r'^articles/(\d{4})/$', 'news.views.year_archive'), url(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'), url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'), - ) + ] Notes: @@ -129,14 +129,14 @@ is ``(?P<name>pattern)``, where ``name`` is the name of the group and Here's the above example URLconf, rewritten to use named groups:: - from django.conf.urls import patterns, url + from django.conf.urls import url - urlpatterns = patterns('', + urlpatterns = [ url(r'^articles/2003/$', 'news.views.special_case_2003'), url(r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'), url(r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'news.views.month_archive'), url(r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$', 'news.views.article_detail'), - ) + ] This accomplishes exactly the same thing as the previous example, with one subtle difference: The captured values are passed to view functions as keyword @@ -203,12 +203,12 @@ A convenient trick is to specify default parameters for your views' arguments. Here's an example URLconf and view:: # URLconf - from django.conf.urls import patterns, url + from django.conf.urls import url - urlpatterns = patterns('', + urlpatterns = [ url(r'^blog/$', 'blog.views.page'), url(r'^blog/page(?P<num>\d+)/$', 'blog.views.page'), - ) + ] # View (in blog/views.py) def page(request, num="1"): @@ -230,9 +230,8 @@ accessed. This makes the system blazingly fast. Syntax of the urlpatterns variable ================================== -``urlpatterns`` should be a Python list, in the format returned by the function -:func:`django.conf.urls.patterns`. Always use ``patterns()`` to create -the ``urlpatterns`` variable. +``urlpatterns`` should be a Python list of :func:`~django.conf.urls.url` +instances. Error handling ============== @@ -260,73 +259,6 @@ The variables are: * ``handler403`` -- See :data:`django.conf.urls.handler403`. * ``handler400`` -- See :data:`django.conf.urls.handler400`. -.. _urlpatterns-view-prefix: - -The view prefix -=============== - -You can specify a common prefix in your ``patterns()`` call, to cut down on -code duplication. - -Here's the example URLconf from the :doc:`Django overview </intro/overview>`:: - - from django.conf.urls import patterns, url - - urlpatterns = patterns('', - url(r'^articles/(\d{4})/$', 'news.views.year_archive'), - url(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'), - url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'), - ) - -In this example, each view has a common prefix -- ``'news.views'``. -Instead of typing that out for each entry in ``urlpatterns``, you can use the -first argument to the ``patterns()`` function to specify a prefix to apply to -each view function. - -With this in mind, the above example can be written more concisely as:: - - from django.conf.urls import patterns, url - - urlpatterns = patterns('news.views', - url(r'^articles/(\d{4})/$', 'year_archive'), - url(r'^articles/(\d{4})/(\d{2})/$', 'month_archive'), - url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'article_detail'), - ) - -Note that you don't put a trailing dot (``"."``) in the prefix. Django puts -that in automatically. - -Multiple view prefixes ----------------------- - -In practice, you'll probably end up mixing and matching views to the point -where the views in your ``urlpatterns`` won't have a common prefix. However, -you can still take advantage of the view prefix shortcut to remove duplication. -Just add multiple ``patterns()`` objects together, like this: - -Old:: - - from django.conf.urls import patterns, url - - urlpatterns = patterns('', - url(r'^$', 'myapp.views.app_index'), - url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$', 'myapp.views.month_display'), - url(r'^tag/(?P<tag>\w+)/$', 'weblog.views.tag'), - ) - -New:: - - from django.conf.urls import patterns, url - - urlpatterns = patterns('myapp.views', - url(r'^$', 'app_index'), - url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$','month_display'), - ) - - urlpatterns += patterns('weblog.views', - url(r'^tag/(?P<tag>\w+)/$', 'tag'), - ) - .. _including-other-urlconfs: Including other URLconfs @@ -338,14 +270,14 @@ essentially "roots" a set of URLs below other ones. For example, here's an excerpt of the URLconf for the `Django Web site`_ itself. It includes a number of other URLconfs:: - from django.conf.urls import include, patterns, url + from django.conf.urls import include, url - urlpatterns = patterns('', + urlpatterns = [ # ... snip ... url(r'^community/', include('django_website.aggregator.urls')), url(r'^contact/', include('django_website.contact.urls')), # ... snip ... - ) + ] Note that the regular expressions in this example don't have a ``$`` (end-of-string match character) but do include a trailing slash. Whenever @@ -353,23 +285,21 @@ Django encounters ``include()`` (:func:`django.conf.urls.include()`), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing. -Another possibility is to include additional URL patterns not by specifying the -URLconf Python module defining them as the ``include()`` argument but by using -directly the pattern list as returned by :func:`~django.conf.urls.patterns` -instead. For example, consider this URLconf:: +Another possibility is to include additional URL patterns by using a list of +:func:`~django.conf.urls.url` instances. For example, consider this URLconf:: - from django.conf.urls import include, patterns, url + from django.conf.urls import include, url - extra_patterns = patterns('', + extra_patterns = [ url(r'^reports/(?P<id>\d+)/$', 'credit.views.report'), url(r'^charge/$', 'credit.views.charge'), - ) + ] - urlpatterns = patterns('', + urlpatterns = [ url(r'^$', 'apps.main.views.homepage'), url(r'^help/', include('apps.help.urls')), url(r'^credit/', include(extra_patterns)), - ) + ] In this example, the ``/credit/reports/`` URL will be handled by the ``credit.views.report()`` Django view. @@ -377,28 +307,30 @@ In this example, the ``/credit/reports/`` URL will be handled by the This can be used to remove redundancy from URLconfs where a single pattern prefix is used repeatedly. For example, consider this URLconf:: - from django.conf.urls import patterns, url + from django.conf.urls import url + from . import views - urlpatterns = patterns('wiki.views', - url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/history/$', 'history'), - url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/edit/$', 'edit'), - url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/discuss/$', 'discuss'), - url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/permissions/$', 'permissions'), - ) + urlpatterns = [ + url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/history/$', views.history), + url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/edit/$', views.edit), + url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/discuss/$', views.discuss), + url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/permissions/$', views.permissions), + ] We can improve this by stating the common path prefix only once and grouping the suffixes that differ:: - from django.conf.urls import include, patterns, url + from django.conf.urls import include, url + from . import views - urlpatterns = patterns('', - url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/', include(patterns('wiki.views', - url(r'^history/$', 'history'), - url(r'^edit/$', 'edit'), - url(r'^discuss/$', 'discuss'), - url(r'^permissions/$', 'permissions'), - ))), - ) + urlpatterns = [ + url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/', include([ + url(r'^history/$', views.history), + url(r'^edit/$', views.edit), + url(r'^discuss/$', views.discuss), + url(r'^permissions/$', views.permissions), + ])), + ] .. _`Django Web site`: https://www.djangoproject.com/ @@ -409,19 +341,20 @@ An included URLconf receives any captured parameters from parent URLconfs, so the following example is valid:: # In settings/urls/main.py - from django.conf.urls import include, patterns, url + from django.conf.urls import include, url - urlpatterns = patterns('', + urlpatterns = [ url(r'^(?P<username>\w+)/blog/', include('foo.urls.blog')), - ) + ] # In foo/urls/blog.py - from django.conf.urls import patterns, url + from django.conf.urls import url + from . import views - urlpatterns = patterns('foo.views', - url(r'^$', 'blog.index'), - url(r'^archive/$', 'blog.archive'), - ) + urlpatterns = [ + url(r'^$', views.blog.index), + url(r'^archive/$', views.blog.archive), + ] In the above example, the captured ``"username"`` variable is passed to the included URLconf, as expected. @@ -440,11 +373,12 @@ function. For example:: - from django.conf.urls import patterns, url + from django.conf.urls import url + from . import views - urlpatterns = patterns('blog.views', - url(r'^blog/(?P<year>\d{4})/$', 'year_archive', {'foo': 'bar'}), - ) + urlpatterns = [ + url(r'^blog/(?P<year>\d{4})/$', views.year_archive, {'foo': 'bar'}), + ] In this example, for a request to ``/blog/2005/``, Django will call ``blog.views.year_archive(request, year='2005', foo='bar')``. @@ -472,36 +406,38 @@ For example, these two URLconf sets are functionally identical: Set one:: # main.py - from django.conf.urls import include, patterns, url + from django.conf.urls import include, url - urlpatterns = patterns('', + urlpatterns = [ url(r'^blog/', include('inner'), {'blogid': 3}), - ) + ] # inner.py - from django.conf.urls import patterns, url + from django.conf.urls import url + from mysite import views - urlpatterns = patterns('', - url(r'^archive/$', 'mysite.views.archive'), - url(r'^about/$', 'mysite.views.about'), - ) + urlpatterns = [ + url(r'^archive/$', views.archive), + url(r'^about/$', views.about), + ] Set two:: # main.py - from django.conf.urls import include, patterns, url + from django.conf.urls import include, url + from mysite import views - urlpatterns = patterns('', + urlpatterns = [ url(r'^blog/', include('inner')), - ) + ] # inner.py - from django.conf.urls import patterns, url + from django.conf.urls import url - urlpatterns = patterns('', - url(r'^archive/$', 'mysite.views.archive', {'blogid': 3}), - url(r'^about/$', 'mysite.views.about', {'blogid': 3}), - ) + urlpatterns = [ + url(r'^archive/$', views.archive, {'blogid': 3}), + url(r'^about/$', views.about, {'blogid': 3}), + ] Note that extra options will *always* be passed to *every* line in the included URLconf, regardless of whether the line's view actually accepts those options @@ -517,38 +453,38 @@ supported -- you can pass any callable object as the view. For example, given this URLconf in "string" notation:: - from django.conf.urls import patterns, url + from django.conf.urls import url - urlpatterns = patterns('', + urlpatterns = [ url(r'^archive/$', 'mysite.views.archive'), url(r'^about/$', 'mysite.views.about'), url(r'^contact/$', 'mysite.views.contact'), - ) + ] You can accomplish the same thing by passing objects rather than strings. Just be sure to import the objects:: - from django.conf.urls import patterns, url + from django.conf.urls import url from mysite.views import archive, about, contact - urlpatterns = patterns('', + urlpatterns = [ url(r'^archive/$', archive), url(r'^about/$', about), url(r'^contact/$', contact), - ) + ] The following example is functionally identical. It's just a bit more compact because it imports the module that contains the views, rather than importing each view individually:: - from django.conf.urls import patterns, url + from django.conf.urls import url from mysite import views - urlpatterns = patterns('', + urlpatterns = [ url(r'^archive/$', views.archive), url(r'^about/$', views.about), url(r'^contact/$', views.contact), - ) + ] The style you use is up to you. @@ -558,12 +494,12 @@ the view prefix (as explained in "The view prefix" above) will have no effect. Note that :doc:`class based views</topics/class-based-views/index>` must be imported:: - from django.conf.urls import patterns, url + from django.conf.urls import url from mysite.views import ClassBasedView - urlpatterns = patterns('', + urlpatterns = [ url(r'^myview/$', ClassBasedView.as_view()), - ) + ] Reverse resolution of URLs ========================== @@ -618,13 +554,13 @@ Examples Consider again this URLconf entry:: - from django.conf.urls import patterns, url + from django.conf.urls import url - urlpatterns = patterns('', + urlpatterns = [ #... url(r'^articles/(\d{4})/$', 'news.views.year_archive'), #... - ) + ] According to this design, the URL for the archive corresponding to year *nnnn* is ``/articles/nnnn/``. @@ -670,13 +606,13 @@ It's fairly common to use the same view function in multiple URL patterns in your URLconf. For example, these two URL patterns both point to the ``archive`` view:: - from django.conf.urls import patterns, url + from django.conf.urls import url from mysite.views import archive - urlpatterns = patterns('', + urlpatterns = [ url(r'^archive/(\d{4})/$', archive), url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}), - ) + ] This is completely valid, but it leads to problems when you try to do reverse URL matching (through the :func:`~django.core.urlresolvers.reverse` function @@ -691,13 +627,13 @@ matching. Here's the above example, rewritten to use named URL patterns:: - from django.conf.urls import patterns, url + from django.conf.urls import url from mysite.views import archive - urlpatterns = patterns('', + urlpatterns = [ url(r'^archive/(\d{4})/$', archive, name="full-archive"), url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}, name="arch-summary"), - ) + ] With these names in place (``full-archive`` and ``arch-summary``), you can target each pattern individually by using its name: @@ -859,20 +795,20 @@ This will include the URLs defined in ``apps.help.urls`` into the ``'foo'``. Secondly, you can include an object that contains embedded namespace data. If -you ``include()`` an object as returned by :func:`~django.conf.urls.patterns`, +you ``include()`` a list of :func:`django.conf.urls.url` instances, the URLs contained in that object will be added to the global namespace. However, you can also ``include()`` a 3-tuple containing:: - (<patterns object>, <application namespace>, <instance namespace>) + (<list of url() instances>, <application namespace>, <instance namespace>) For example:: - from django.conf.urls import include, patterns, url + from django.conf.urls import include, url - help_patterns = patterns('', + help_patterns = [ url(r'^basic/$', 'apps.help.views.views.basic'), url(r'^advanced/$', 'apps.help.views.views.advanced'), - ) + ] url(r'^help/', include((help_patterns, 'bar', 'foo'))), diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt index 16742ab622..3d48344643 100644 --- a/docs/topics/i18n/translation.txt +++ b/docs/topics/i18n/translation.txt @@ -911,13 +911,15 @@ URL. Paths listed in :setting:`LOCALE_PATHS` are also included. You hook it up like this:: + from django.views.i18n import javascript_catalog + js_info_dict = { 'packages': ('your.app.package',), } - urlpatterns = patterns('', - (r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict), - ) + urlpatterns = [ + url(r'^jsi18n/$', javascript_catalog, js_info_dict), + ] Each string in ``packages`` should be in Python dotted-package syntax (the same format as the strings in :setting:`INSTALLED_APPS`) and should refer to a @@ -935,9 +937,9 @@ changed by altering the ``domain`` argument. You can make the view dynamic by putting the packages into the URL pattern:: - urlpatterns = patterns('', - (r'^jsi18n/(?P<packages>\S+?)/$', 'django.views.i18n.javascript_catalog'), - ) + urlpatterns = [ + url(r'^jsi18n/(?P<packages>\S+?)/$', javascript_catalog), + ] With this, you specify the packages as a list of package names delimited by '+' signs in the URL. This is especially useful if your pages use code from @@ -1085,25 +1087,30 @@ Language prefix in URL patterns .. function:: i18n_patterns(prefix, pattern_description, ...) -This function can be used in your root URLconf as a replacement for the normal -:func:`django.conf.urls.patterns` function. Django will automatically +.. deprecated:: 1.8 + + The ``prefix`` argument to ``i18n_patterns()`` has been deprecated and will + not be supported in Django 2.0. Simply pass a list of + :func:`django.conf.urls.url` instances instead. + +This function can be used in your root URLconf and Django will automatically prepend the current active language code to all url patterns defined within :func:`~django.conf.urls.i18n.i18n_patterns`. Example URL patterns:: - from django.conf.urls import patterns, include, url + from django.conf.urls import include, url from django.conf.urls.i18n import i18n_patterns - urlpatterns = patterns('', + urlpatterns = [ url(r'^sitemap\.xml$', 'sitemap.view', name='sitemap_xml'), - ) + ] - news_patterns = patterns('', + news_patterns = [ url(r'^$', 'news.views.index', name='index'), url(r'^category/(?P<slug>[\w-]+)/$', 'news.views.category', name='category'), url(r'^(?P<slug>[\w-]+)/$', 'news.views.details', name='detail'), - ) + ] - urlpatterns += i18n_patterns('', + urlpatterns += i18n_patterns( url(r'^about/$', 'about.view', name='about'), url(r'^news/', include(news_patterns, namespace='news')), ) @@ -1144,21 +1151,21 @@ Translating URL patterns URL patterns can also be marked translatable using the :func:`~django.utils.translation.ugettext_lazy` function. Example:: - from django.conf.urls import patterns, include, url + from django.conf.urls import include, url from django.conf.urls.i18n import i18n_patterns from django.utils.translation import ugettext_lazy as _ - urlpatterns = patterns('' + urlpatterns = [ url(r'^sitemap\.xml$', 'sitemap.view', name='sitemap_xml'), - ) + ] - news_patterns = patterns('' + news_patterns = [ url(r'^$', 'news.views.index', name='index'), url(_(r'^category/(?P<slug>[\w-]+)/$'), 'news.views.category', name='category'), url(r'^(?P<slug>[\w-]+)/$', 'news.views.details', name='detail'), - ) + ] - urlpatterns += i18n_patterns('', + urlpatterns += i18n_patterns( url(_(r'^about/$'), 'about.view', name='about'), url(_(r'^news/'), include(news_patterns, namespace='news')), ) |
