Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:python:django:sites [2020/04/22 08:49] alfred [Current site] |
wiki2:python:django:sites [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 61: | Línea 61: | ||
| </code> | </code> | ||
| + | ==== The middleware ==== | ||
| + | To add the ''site'' attribute to the ''request'' for a view just add ''django.contrib.sites.middleware.CurrentSiteMiddleware'' to ''MIDDLEWARE''. | ||
| ==== The full url ==== | ==== The full url ==== | ||
| <code python> | <code python> | ||
| Línea 103: | Línea 105: | ||
| If you wanted to choose different templates refer to [[https://docs.djangoproject.com/en/3.0/ref/contrib/sites/#getting-the-current-domain-for-display|this link]]. | If you wanted to choose different templates refer to [[https://docs.djangoproject.com/en/3.0/ref/contrib/sites/#getting-the-current-domain-for-display|this link]]. | ||
| + | ==== Querying depending on the site ==== | ||
| + | <code python> | ||
| + | from django.contrib.sites.models import Site | ||
| + | from django.contrib.sites.managers import CurrentSiteManager | ||
| + | from django.db import models | ||
| + | |||
| + | class Photo(models.Model): | ||
| + | photo = models.FileField(upload_to='photos') | ||
| + | photographer_name = models.CharField(max_length=100) | ||
| + | pub_date = models.DateField() | ||
| + | site = models.ForeignKey(Site, on_delete=models.CASCADE) | ||
| + | objects = models.Manager() | ||
| + | on_site = CurrentSiteManager() | ||
| + | </code> | ||
| + | |||
| + | Having this we can: | ||
| + | * Query all Photo objects in the database with ''Photo.objects.all()''. | ||
| + | * Query only the Photo objects associated with the current site (according to the ''SITE_ID''): ''Photo.on_site.all()'' | ||