diff --git a/django/contrib/sites/middleware.py b/django/contrib/sites/middleware.py
new file mode 100644
index 0000000..74ce561
-
|
+
|
|
| 1 | from django.conf import settings |
| 2 | from django.contrib.sites.models import Site |
| 3 | |
| 4 | |
| 5 | class SiteMiddleware(object): |
| 6 | """ |
| 7 | Middleware that sets `site` attribute to request object. |
| 8 | """ |
| 9 | |
| 10 | def process_request(self, request): |
| 11 | request.site = Site.objects.get_current() |
diff --git a/django/contrib/sites/tests.py b/django/contrib/sites/tests.py
index 17ab1f2..3a80ffc 100644
a
|
b
|
|
1 | 1 | from django.conf import settings |
2 | 2 | from django.contrib.sites.models import Site, RequestSite, get_current_site |
| 3 | from django.contrib.sites.middleware import SiteMiddleware |
3 | 4 | from django.core.exceptions import ObjectDoesNotExist |
4 | 5 | from django.http import HttpRequest |
5 | 6 | from django.test import TestCase |
… |
… |
class SitesFrameworkTests(TestCase):
|
54 | 55 | site = get_current_site(request) |
55 | 56 | self.assertTrue(isinstance(site, RequestSite)) |
56 | 57 | self.assertEqual(site.name, u"example.com") |
| 58 | |
| 59 | class MiddlewareTest(TestCase): |
| 60 | |
| 61 | def test_request(self): |
| 62 | """ |
| 63 | Makes sure that the request has correct `site` attribute. |
| 64 | """ |
| 65 | |
| 66 | middleware = SiteMiddleware() |
| 67 | request = HttpRequest() |
| 68 | middleware.process_request(request) |
| 69 | self.assertEqual(request.site.id, settings.SITE_ID) |
diff --git a/docs/ref/contrib/sites.txt b/docs/ref/contrib/sites.txt
index 8fc434b..7a9f340 100644
a
|
b
|
fallback for cases where it is not installed.
|
174 | 174 | .. function:: get_current_site(request) |
175 | 175 | |
176 | 176 | Checks if contrib.sites is installed and returns either the current |
177 | | :class:`~django.contrib.sites.models.Site` object or a |
| 177 | :class:`~django.contrib.sites.models.Site` object or a |
178 | 178 | :class:`~django.contrib.sites.models.RequestSite` object based on |
179 | 179 | the request. |
180 | 180 | |
… |
… |
fallback when the database-backed sites framework is not available.
|
437 | 437 | |
438 | 438 | Sets the ``name`` and ``domain`` attributes to the value of |
439 | 439 | :meth:`~django.http.HttpRequest.get_host`. |
440 | | |
| 440 | |
441 | 441 | |
442 | 442 | A :class:`~django.contrib.sites.models.RequestSite` object has a similar |
443 | 443 | interface to a normal :class:`~django.contrib.sites.models.Site` object, except |
… |
… |
its :meth:`~django.contrib.sites.models.RequestSite.__init__()` method takes an
|
447 | 447 | and ``delete()`` methods to match the interface of |
448 | 448 | :class:`~django.contrib.sites.models.Site`, but the methods raise |
449 | 449 | ``NotImplementedError``. |
| 450 | |
| 451 | ``SiteMiddleware`` |
| 452 | ================== |
| 453 | |
| 454 | If you often use this pattern:: |
| 455 | |
| 456 | from django.contrib.sites.models import Site |
| 457 | |
| 458 | def my_view(request): |
| 459 | site = Site.objects.get_current() |
| 460 | ... |
| 461 | |
| 462 | there is simple way to avoid repetitions. Add |
| 463 | :mod:`django.contrib.site.middleware.SiteMiddleware` |
| 464 | to :setting:`MIDDLEWARE_CLASSES`. The middleware sets ``site`` attribute |
| 465 | to every incoming request object. |
diff --git a/docs/ref/middleware.txt b/docs/ref/middleware.txt
index 737e0b2..570a33a 100644
a
|
b
|
Message middleware
|
151 | 151 | Enables cookie- and session-based message support. See the |
152 | 152 | :doc:`messages documentation </ref/contrib/messages>`. |
153 | 153 | |
| 154 | Site middleware |
| 155 | ---------------- |
| 156 | |
| 157 | .. module:: django.contrib.site.middleware |
| 158 | :synopsis: Site middleware. |
| 159 | |
| 160 | .. class:: SiteMiddleware |
| 161 | |
| 162 | .. versionadded:: 1.4 |
| 163 | |
| 164 | Adds the ``site`` attribute, representing the current site, to every incoming |
| 165 | ``HttpRequest`` object. See the :doc:`sites documentation </ref/contrib/sites>`. |
| 166 | |
154 | 167 | Session middleware |
155 | 168 | ------------------ |
156 | 169 | |