Opened 9 years ago

Closed 8 years ago

Last modified 7 years ago

#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


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()
    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)

13814.diff (676 bytes) - added by Yuval Adam 9 years ago.
Proposed utility method

Download all attachments as: .zip

Change History (5)

comment:1 Changed 9 years ago by Yuval Adam

milestone: 1.3
Owner: changed from nobody to Yuval Adam

comment:2 Changed 9 years ago by Yuval Adam

Has patch: set

Changed 9 years ago by Yuval Adam

Attachment: 13814.diff added

Proposed utility method

comment:3 Changed 8 years ago by Gabriel Hurley

Resolution: duplicate
Status: newclosed

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.

comment:4 Changed 7 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

Note: See TracTickets for help on using tickets.
Back to Top