mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #22218 -- Deprecated django.conf.urls.patterns.
Thanks Carl Meyer for the suggestion and Alex Gaynor and Carl for reviews.
This commit is contained in:
parent
e6ced2bb08
commit
d73d0e071c
117 changed files with 1180 additions and 1099 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
--------------------
|
||||
|
|
|
@ -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
|
||||
=======================================
|
||||
|
|
|
@ -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:
|
||||
|
||||
|
|
|
@ -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`
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
--------------------
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue