sites.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. .. _ref-contrib-sites:
  2. =====================
  3. The "sites" framework
  4. =====================
  5. .. module:: django.contrib.sites
  6. :synopsis: Lets you operate multiple web sites from the same database and
  7. Django project
  8. Django comes with an optional "sites" framework. It's a hook for associating
  9. objects and functionality to particular Web sites, and it's a holding place for
  10. the domain names and "verbose" names of your Django-powered sites.
  11. Use it if your single Django installation powers more than one site and you
  12. need to differentiate between those sites in some way.
  13. The whole sites framework is based on a simple model:
  14. .. class:: django.contrib.sites.models.Site
  15. This model has :attr:`~django.contrib.sites.models.Site.domain` and
  16. :attr:`~django.contrib.sites.models.Site.name` fields. The :setting:`SITE_ID`
  17. setting specifies the database ID of the
  18. :class:`~django.contrib.sites.models.Site` object associated with that
  19. particular settings file.
  20. How you use this is up to you, but Django uses it in a couple of ways
  21. automatically via simple conventions.
  22. Example usage
  23. =============
  24. Why would you use sites? It's best explained through examples.
  25. Associating content with multiple sites
  26. ---------------------------------------
  27. The Django-powered sites LJWorld.com_ and Lawrence.com_ are operated by the
  28. same news organization -- the Lawrence Journal-World newspaper in Lawrence,
  29. Kansas. LJWorld.com focuses on news, while Lawrence.com focuses on local
  30. entertainment. But sometimes editors want to publish an article on *both*
  31. sites.
  32. The brain-dead way of solving the problem would be to require site producers to
  33. publish the same story twice: once for LJWorld.com and again for Lawrence.com.
  34. But that's inefficient for site producers, and it's redundant to store
  35. multiple copies of the same story in the database.
  36. The better solution is simple: Both sites use the same article database, and an
  37. article is associated with one or more sites. In Django model terminology,
  38. that's represented by a :class:`~django.db.models.ManyToManyField` in the
  39. ``Article`` model::
  40. from django.db import models
  41. from django.contrib.sites.models import Site
  42. class Article(models.Model):
  43. headline = models.CharField(max_length=200)
  44. # ...
  45. sites = models.ManyToManyField(Site)
  46. This accomplishes several things quite nicely:
  47. * It lets the site producers edit all content -- on both sites -- in a
  48. single interface (the Django admin).
  49. * It means the same story doesn't have to be published twice in the
  50. database; it only has a single record in the database.
  51. * It lets the site developers use the same Django view code for both sites.
  52. The view code that displays a given story just checks to make sure the
  53. requested story is on the current site. It looks something like this::
  54. from django.conf import settings
  55. def article_detail(request, article_id):
  56. try:
  57. a = Article.objects.get(id=article_id, sites__id__exact=settings.SITE_ID)
  58. except Article.DoesNotExist:
  59. raise Http404
  60. # ...
  61. .. _ljworld.com: http://www.ljworld.com/
  62. .. _lawrence.com: http://www.lawrence.com/
  63. Associating content with a single site
  64. --------------------------------------
  65. Similarly, you can associate a model to the :class:`~django.contrib.sites.models.Site`
  66. model in a many-to-one relationship, using
  67. :class:`~django.db.models.fields.related.ForeignKey`.
  68. For example, if an article is only allowed on a single site, you'd use a model
  69. like this::
  70. from django.db import models
  71. from django.contrib.sites.models import Site
  72. class Article(models.Model):
  73. headline = models.CharField(max_length=200)
  74. # ...
  75. site = models.ForeignKey(Site)
  76. This has the same benefits as described in the last section.
  77. Hooking into the current site from views
  78. ----------------------------------------
  79. On a lower level, you can use the sites framework in your Django views to do
  80. particular things based on the site in which the view is being called.
  81. For example::
  82. from django.conf import settings
  83. def my_view(request):
  84. if settings.SITE_ID == 3:
  85. # Do something.
  86. else:
  87. # Do something else.
  88. Of course, it's ugly to hard-code the site IDs like that. This sort of
  89. hard-coding is best for hackish fixes that you need done quickly. A slightly
  90. cleaner way of accomplishing the same thing is to check the current site's
  91. domain::
  92. from django.conf import settings
  93. from django.contrib.sites.models import Site
  94. def my_view(request):
  95. current_site = Site.objects.get(id=settings.SITE_ID)
  96. if current_site.domain == 'foo.com':
  97. # Do something
  98. else:
  99. # Do something else.
  100. The idiom of retrieving the :class:`~django.contrib.sites.models.Site` object
  101. for the value of :setting:`settings.SITE_ID <SITE_ID>` is quite common, so
  102. the :class:`~django.contrib.sites.models.Site` model's manager has a
  103. ``get_current()`` method. This example is equivalent to the previous one::
  104. from django.contrib.sites.models import Site
  105. def my_view(request):
  106. current_site = Site.objects.get_current()
  107. if current_site.domain == 'foo.com':
  108. # Do something
  109. else:
  110. # Do something else.
  111. Getting the current domain for display
  112. --------------------------------------
  113. LJWorld.com and Lawrence.com both have e-mail alert functionality, which lets
  114. readers sign up to get notifications when news happens. It's pretty basic: A
  115. reader signs up on a Web form, and he immediately gets an e-mail saying,
  116. "Thanks for your subscription."
  117. It'd be inefficient and redundant to implement this signup-processing code
  118. twice, so the sites use the same code behind the scenes. But the "thank you for
  119. signing up" notice needs to be different for each site. By using
  120. :class:`~django.contrib.sites.models.Site`
  121. objects, we can abstract the "thank you" notice to use the values of the
  122. current site's :attr:`~django.contrib.sites.models.Site.name` and
  123. :attr:`~django.contrib.sites.models.Site.domain`.
  124. Here's an example of what the form-handling view looks like::
  125. from django.contrib.sites.models import Site
  126. from django.core.mail import send_mail
  127. def register_for_newsletter(request):
  128. # Check form values, etc., and subscribe the user.
  129. # ...
  130. current_site = Site.objects.get_current()
  131. send_mail('Thanks for subscribing to %s alerts' % current_site.name,
  132. 'Thanks for your subscription. We appreciate it.\n\n-The %s team.' % current_site.name,
  133. 'editor@%s' % current_site.domain,
  134. [user.email])
  135. # ...
  136. On Lawrence.com, this e-mail has the subject line "Thanks for subscribing to
  137. lawrence.com alerts." On LJWorld.com, the e-mail has the subject "Thanks for
  138. subscribing to LJWorld.com alerts." Same goes for the e-mail's message body.
  139. Note that an even more flexible (but more heavyweight) way of doing this would
  140. be to use Django's template system. Assuming Lawrence.com and LJWorld.com have
  141. different template directories (:setting:`TEMPLATE_DIRS`), you could simply farm out
  142. to the template system like so::
  143. from django.core.mail import send_mail
  144. from django.template import loader, Context
  145. def register_for_newsletter(request):
  146. # Check form values, etc., and subscribe the user.
  147. # ...
  148. subject = loader.get_template('alerts/subject.txt').render(Context({}))
  149. message = loader.get_template('alerts/message.txt').render(Context({}))
  150. send_mail(subject, message, 'editor@ljworld.com', [user.email])
  151. # ...
  152. In this case, you'd have to create :file:`subject.txt` and :file:`message.txt` template
  153. files for both the LJWorld.com and Lawrence.com template directories. That
  154. gives you more flexibility, but it's also more complex.
  155. It's a good idea to exploit the :class:`~django.contrib.sites.models.Site``
  156. objects as much as possible, to remove unneeded complexity and redundancy.
  157. Getting the current domain for full URLs
  158. ----------------------------------------
  159. Django's ``get_absolute_url()`` convention is nice for getting your objects'
  160. URL without the domain name, but in some cases you might want to display the
  161. full URL -- with ``http://`` and the domain and everything -- for an object.
  162. To do this, you can use the sites framework. A simple example::
  163. >>> from django.contrib.sites.models import Site
  164. >>> obj = MyModel.objects.get(id=3)
  165. >>> obj.get_absolute_url()
  166. '/mymodel/objects/3/'
  167. >>> Site.objects.get_current().domain
  168. 'example.com'
  169. >>> 'http://%s%s' % (Site.objects.get_current().domain, obj.get_absolute_url())
  170. 'http://example.com/mymodel/objects/3/'
  171. Caching the current ``Site`` object
  172. ===================================
  173. .. versionadded:: 1.0
  174. As the current site is stored in the database, each call to
  175. ``Site.objects.get_current()`` could result in a database query. But Django is a
  176. little cleverer than that: on the first request, the current site is cached, and
  177. any subsequent call returns the cached data instead of hitting the database.
  178. If for any reason you want to force a database query, you can tell Django to
  179. clear the cache using ``Site.objects.clear_cache()``::
  180. # First call; current site fetched from database.
  181. current_site = Site.objects.get_current()
  182. # ...
  183. # Second call; current site fetched from cache.
  184. current_site = Site.objects.get_current()
  185. # ...
  186. # Force a database query for the third call.
  187. Site.objects.clear_cache()
  188. current_site = Site.objects.get_current()
  189. The ``CurrentSiteManager``
  190. ==========================
  191. .. class:: django.contrib.sites.managers.CurrentSiteManager
  192. If :class:`~django.contrib.sites.models.Site`\s play a key role in your application,
  193. consider using the helpful
  194. :class:`~django.contrib.sites.managers.CurrentSiteManager` in your model(s).
  195. It's a model :ref:`manager <topics-db-managers>` that automatically filters
  196. its queries to include only objects associated with the current
  197. :class:`~django.contrib.sites.models.Site`.
  198. Use :class:`~django.contrib.sites.managers.CurrentSiteManager` by adding it to
  199. your model explicitly. For example::
  200. from django.db import models
  201. from django.contrib.sites.models import Site
  202. from django.contrib.sites.managers import CurrentSiteManager
  203. class Photo(models.Model):
  204. photo = models.FileField(upload_to='/home/photos')
  205. photographer_name = models.CharField(max_length=100)
  206. pub_date = models.DateField()
  207. site = models.ForeignKey(Site)
  208. objects = models.Manager()
  209. on_site = CurrentSiteManager()
  210. With this model, ``Photo.objects.all()`` will return all ``Photo`` objects in
  211. the database, but ``Photo.on_site.all()`` will return only the ``Photo`` objects
  212. associated with the current site, according to the :setting:`SITE_ID` setting.
  213. Put another way, these two statements are equivalent::
  214. Photo.objects.filter(site=settings.SITE_ID)
  215. Photo.on_site.all()
  216. How did :class:`~django.contrib.sites.managers.CurrentSiteManager` know which
  217. field of ``Photo`` was the :class:`~django.contrib.sites.models.Site`? It
  218. defaults to looking for a field called
  219. :class:`~django.contrib.sites.models.Site`. If your model has a
  220. :class:`~django.db.models.fields.related.ForeignKey` or
  221. :class:`~django.db.models.fields.related.ManyToManyField` called something
  222. *other* than :class:`~django.contrib.sites.models.Site`, you need to explicitly
  223. pass that as the parameter to
  224. :class:`~django.contrib.sites.managers.CurrentSiteManager`. The following model,
  225. which has a field called ``publish_on``, demonstrates this::
  226. from django.db import models
  227. from django.contrib.sites.models import Site
  228. from django.contrib.sites.managers import CurrentSiteManager
  229. class Photo(models.Model):
  230. photo = models.FileField(upload_to='/home/photos')
  231. photographer_name = models.CharField(max_length=100)
  232. pub_date = models.DateField()
  233. publish_on = models.ForeignKey(Site)
  234. objects = models.Manager()
  235. on_site = CurrentSiteManager('publish_on')
  236. If you attempt to use :class:`~django.contrib.sites.managers.CurrentSiteManager`
  237. and pass a field name that doesn't exist, Django will raise a :exc:`ValueError`.
  238. Finally, note that you'll probably want to keep a normal (non-site-specific)
  239. ``Manager`` on your model, even if you use
  240. :class:`~django.contrib.sites.managers.CurrentSiteManager`. As explained
  241. in the :ref:`manager documentation <topics-db-managers>`, if you define a manager
  242. manually, then Django won't create the automatic ``objects = models.Manager()``
  243. manager for you.Also, note that certain parts of Django -- namely, the Django admin site and
  244. generic views -- use whichever manager is defined *first* in the model, so if
  245. you want your admin site to have access to all objects (not just site-specific
  246. ones), put ``objects = models.Manager()`` in your model, before you define
  247. :class:`~django.contrib.sites.managers.CurrentSiteManager`.
  248. How Django uses the sites framework
  249. ===================================
  250. Although it's not required that you use the sites framework, it's strongly
  251. encouraged, because Django takes advantage of it in a few places. Even if your
  252. Django installation is powering only a single site, you should take the two
  253. seconds to create the site object with your ``domain`` and ``name``, and point
  254. to its ID in your :setting:`SITE_ID` setting.
  255. Here's how Django uses the sites framework:
  256. * In the :mod:`redirects framework <django.contrib.redirects>`, each
  257. redirect object is associated with a particular site. When Django searches
  258. for a redirect, it takes into account the current :setting:`SITE_ID`.
  259. * In the comments framework, each comment is associated with a particular
  260. site. When a comment is posted, its
  261. :class:`~django.contrib.sites.models.Site` is set to the current
  262. :setting:`SITE_ID`, and when comments are listed via the appropriate
  263. template tag, only the comments for the current site are displayed.
  264. * In the :mod:`flatpages framework <django.contrib.flatpages>`, each
  265. flatpage is associated with a particular site. When a flatpage is created,
  266. you specify its :class:`~django.contrib.sites.models.Site`, and the
  267. :class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`
  268. checks the current :setting:`SITE_ID` in retrieving flatpages to display.
  269. * In the :mod:`syndication framework <django.contrib.syndication>`, the
  270. templates for ``title`` and ``description`` automatically have access to a
  271. variable ``{{ site }}``, which is the
  272. :class:`~django.contrib.sites.models.Site` object representing the current
  273. site. Also, the hook for providing item URLs will use the ``domain`` from
  274. the current :class:`~django.contrib.sites.models.Site` object if you don't
  275. specify a fully-qualified domain.
  276. * In the :mod:`authentication framework <django.contrib.auth>`, the
  277. :func:`django.contrib.auth.views.login` view passes the current
  278. :class:`~django.contrib.sites.models.Site` name to the template as
  279. ``{{ site_name }}``.
  280. * The shortcut view (:func:`django.views.defaults.shortcut`) uses the domain
  281. of the current :class:`~django.contrib.sites.models.Site` object when
  282. calculating an object's URL.
  283. * In the admin framework, the "view on site" link uses the current
  284. :class:`~django.contrib.sites.models.Site` to work out the domain for the
  285. site that it will redirect to.
  286. ``RequestSite`` objects
  287. =======================
  288. .. _requestsite-objects:
  289. .. versionadded:: 1.0
  290. Some :ref:`django.contrib <ref-contrib-index>` applications take advantage of
  291. the sites framework but are architected in a way that doesn't *require* the
  292. sites framework to be installed in your database. (Some people don't want to, or
  293. just aren't *able* to install the extra database table that the sites framework
  294. requires.) For those cases, the framework provides a
  295. :class:`~django.contrib.sites.models.RequestSite` class, which can be used as a
  296. fallback when the database-backed sites framework is not available.
  297. A :class:`~django.contrib.sites.models.RequestSite` object has a similar
  298. interface to a normal :class:`~django.contrib.sites.models.Site` object, except
  299. its :meth:`~django.contrib.sites.models.RequestSite.__init__()` method takes an
  300. :class:`~django.http.HttpRequest` object. It's able to deduce the
  301. :attr:`~django.contrib.sites.models.RequestSite.domain` and
  302. :attr:`~django.contrib.sites.models.RequestSite.name` by looking at the
  303. request's domain. It has :meth:`~django.contrib.sites.models.RequestSite.save()`
  304. and :meth:`~django.contrib.sites.models.RequestSite.delete()` methods to match
  305. the interface of :class:`~django.contrib.sites.models.Site`, but the methods
  306. raise :exc:`NotImplementedError`.