#13814 closed (duplicate)
Utility method for getting a Site object
| Reported by: | James Bennett | Owned by: | Yuval Adam |
|---|---|---|---|
| Component: | Contrib apps | Version: | 1.2 |
| Severity: | Keywords: | ||
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Fairly often, I find myself writing code which needs a Site and which, as a general best practice, falls back to RequestSite if contrib.sites isn't installed. Which means writing this code, or something like it, over and over:
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(some_request_object)
Given how common this idiom will be for code which wants to properly support the RequestSite fallback, it'd be nice to just have a utility method somewhere in contrib.sites which could just take an HttpRequest object and return either a Site or a RequestSite as appropriate.
Attachments (1)
Change History (5)
comment:1 by , 15 years ago
| milestone: | → 1.3 |
|---|---|
| Owner: | changed from to |
comment:2 by , 15 years ago
| Has patch: | set |
|---|
by , 15 years ago
| Attachment: | 13814.diff added |
|---|
comment:3 by , 15 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
Duplicate of #10235 (which adds a method to do exactly this as part of it's solution), #8960 (which needs this for the admin 'view on site' function), and #10608 (which would add this code to all the contrib apps). I'll bring this up on the Django Dev list to see if we can get a consensus on the solution for all three tickets.
Proposed utility method