Opened 14 years ago

Closed 14 years ago

Last modified 13 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

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)

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

Download all attachments as: .zip

Change History (5)

comment:1 by Yuval Adam, 14 years ago

milestone: 1.3
Owner: changed from nobody to Yuval Adam

comment:2 by Yuval Adam, 14 years ago

Has patch: set

by Yuval Adam, 14 years ago

Attachment: 13814.diff added

Proposed utility method

comment:3 by Gabriel Hurley, 14 years ago

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 by Jacob, 13 years ago

milestone: 1.3

Milestone 1.3 deleted

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