Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#13814 closed (duplicate)

Utility method for getting a Site object

Reported by: ubernostrum Owned by: yuval_a
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: UI/UX:

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_a 5 years ago.
Proposed utility method

Download all attachments as: .zip

Change History (5)

comment:1 Changed 5 years ago by yuval_a

  • milestone set to 1.3
  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to yuval_a
  • Patch needs improvement unset

comment:2 Changed 5 years ago by yuval_a

  • Has patch set

Changed 5 years ago by yuval_a

Proposed utility method

comment:3 Changed 5 years ago by gabrielhurley

  • Resolution set to duplicate
  • Status changed from new to 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.

comment:4 Changed 4 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

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