[Django] #17005: Adding django.contrib.site.middleware ?

13 views
Skip to first unread message

Django

unread,
Oct 6, 2011, 12:50:20 PM10/6/11
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------+--------------------
Reporter: jordan@… | Owner: nobody
Type: New feature | Status: new
Component: contrib.sites | Version:
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
I end up doing something like the following all over my code:


{{{
from django.contrib.sites.models import Site

def my_view(request, template_name="foo.html"):
site = Site.objects.get_current()
...
return render(request, template_name, locals())

}}}

Seems like it would save time (and possibly reduce database requests,
depending on where else in the application the Site object is being used)
if `django.contrib.sites.middleware.SiteMiddleware` was added, which
either set `request.site` to `Site.objects.get_current()` or added `site`
or `current_site` to the `RequestContext`

I just did a search for "Site.objects.get_current" in one of my web apps
and came up with 26 hits. There's really no reason to have to write out
that code so many times. It should just be present as a variable
somewhere.

--
Ticket URL: <https://code.djangoproject.com/ticket/17005>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 7, 2011, 4:50:35 AM10/7/11
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------+------------------------------------
Reporter: jordan@… | Owner: nobody
Type: New feature | Status: new
Component: contrib.sites | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by aaugustin):

* needs_docs: => 0
* needs_better_patch: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


Comment:

I think the cleanest solution is to set `request.site =
Site.objects.get_current()`. I wouldn't use `get_current_site()` because
this middleware doesn't make sense when the `sites` app isn't enabled. The
current site is cached, so the performance impact is negligible.

This middleware will be trivial, but it could be useful, and I don't see
any reason not to include it in Django. It's similar to the `static` and
`media` context processors.

--
Ticket URL: <https://code.djangoproject.com/ticket/17005#comment:1>

Django

unread,
Oct 7, 2011, 8:49:27 AM10/7/11
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------+------------------------------------
Reporter: jordan@… | Owner: nobody
Type: New feature | Status: new
Component: contrib.sites | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by jordan@…):

I'd be happy to be involved with the coding of it.



> It's similar to the static and media context processors."


So do you think it should be made available via a context processor, or as
middleware?

--
Ticket URL: <https://code.djangoproject.com/ticket/17005#comment:2>

Django

unread,
Oct 7, 2011, 8:49:26 AM10/7/11
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------+------------------------------------
Reporter: jordan@… | Owner: nobody
Type: New feature | Status: new
Component: contrib.sites | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by jordan@…):

I'd be happy to be involved with the coding of it.


> It's similar to the static and media context processors."


So do you think it should be made available via a context processor, or as
middleware?

Django

unread,
Oct 7, 2011, 11:24:37 AM10/7/11
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------+------------------------------------
Reporter: jordan@… | Owner: nobody
Type: New feature | Status: new
Component: contrib.sites | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by aaugustin):

A middleware is more appropriate — I expect the current site object to be
used in the view rather than in the template.

The comparison with the static and media context processors was just about
the triviality of the code.

--
Ticket URL: <https://code.djangoproject.com/ticket/17005#comment:3>

Django

unread,
Dec 17, 2011, 12:39:43 PM12/17/11
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------+-------------------------------------
Reporter: jordan@… | Owner: anonymous
Type: New feature | Status: assigned
Component: contrib.sites | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-------------------------------------
Changes (by anonymous):

* owner: nobody => anonymous
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/17005#comment:4>

Django

unread,
Dec 17, 2011, 12:40:04 PM12/17/11
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------+--------------------------------------
Reporter: jordan@… | Owner: krzysiumed
Type: New feature | Status: new
Component: contrib.sites | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by krzysiumed):

* owner: anonymous => krzysiumed
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/17005#comment:5>

Django

unread,
Dec 17, 2011, 12:40:25 PM12/17/11
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------+--------------------------------------
Reporter: jordan@… | Owner: krzysiumed
Type: New feature | Status: assigned
Component: contrib.sites | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by krzysiumed):

* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/17005#comment:6>

Django

unread,
Dec 17, 2011, 1:11:20 PM12/17/11
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------+--------------------------------------
Reporter: jordan@… | Owner: krzysiumed
Type: New feature | Status: assigned
Component: contrib.sites | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by krzysiumed):

I'd written a patch. It contains docs and tests (I believe they are not
necessary, because the middleware is very simple). Sorry for my poor
English in docs.

--
Ticket URL: <https://code.djangoproject.com/ticket/17005#comment:7>

Django

unread,
Dec 17, 2011, 2:48:29 PM12/17/11
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------+--------------------------------------
Reporter: jordan@… | Owner: krzysiumed
Type: New feature | Status: assigned
Component: contrib.sites | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by charettes):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/17005#comment:8>

Django

unread,
Jan 3, 2012, 3:51:10 AM1/3/12
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------+--------------------------------------
Reporter: jordan@… | Owner: krzysiumed
Type: New feature | Status: assigned
Component: contrib.sites | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by krzysiumed):

* cc: krzysiumed@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/17005#comment:9>

Django

unread,
Dec 16, 2012, 4:24:23 PM12/16/12
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------+--------------------------------------
Reporter: jordan@… | Owner: krzysiumed
Type: New feature | Status: assigned
Component: contrib.sites | Version:

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by aaugustin):

#13559 is about adding a context processor for the same purpose in
templates.

--
Ticket URL: <https://code.djangoproject.com/ticket/17005#comment:10>

Django

unread,
Nov 18, 2013, 3:19:16 PM11/18/13
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------+----------------------------------------
Reporter: jordan@… | Owner: chrismedrela

Type: New feature | Status: assigned
Component: contrib.sites | Version:

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+----------------------------------------
Changes (by chrismedrela):

* owner: krzysiumed => chrismedrela


Comment:

I've updated the patch: https://github.com/django/django/pull/1939. BTW,
krzysiumed is my old nick.

--
Ticket URL: <https://code.djangoproject.com/ticket/17005#comment:11>

Django

unread,
Feb 2, 2014, 11:45:53 AM2/2/14
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------------+-------------------------------------
Reporter: jordan@… | Owner:
Type: New feature | chrismedrela
Component: contrib.sites | Status: assigned
Severity: Normal | Version:
Keywords: | Resolution:
Has patch: 1 | Triage Stage: Ready for
Needs tests: 0 | checkin
Easy pickings: 0 | Needs documentation: 0
| Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* stage: Accepted => Ready for checkin


Comment:

The code looks good to me. Can someone review the docs?

--
Ticket URL: <https://code.djangoproject.com/ticket/17005#comment:12>

Django

unread,
Feb 6, 2014, 4:46:30 AM2/6/14
to django-...@googlegroups.com
#17005: Adding django.contrib.site.middleware ?
-------------------------------------+-------------------------------------
Reporter: jordan@… | Owner:
Type: New feature | chrismedrela
Component: contrib.sites | Status: closed
Severity: Normal | Version:
Keywords: | Resolution: fixed

Has patch: 1 | Triage Stage: Ready for
Needs tests: 0 | checkin
Easy pickings: 0 | Needs documentation: 0
| Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"b22d6c47a7e4c7ab26a8b7b033d11fa6743aae86"]:
{{{
#!CommitTicketReference repository=""
revision="b22d6c47a7e4c7ab26a8b7b033d11fa6743aae86"
Fixed #17005 -- Added CurrentSiteMiddleware to set the current site on
each request.

Thanks jordan at aace.org for the suggestion.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/17005#comment:13>

Django

unread,
Feb 6, 2014, 4:53:02 AM2/6/14
to django-...@googlegroups.com
#17005: Add CurrentSiteMiddleware to set the current site on the request

-------------------------------------+-------------------------------------
Reporter: jordan@… | Owner:
Type: New feature | chrismedrela
Component: contrib.sites | Status: closed
Severity: Normal | Version:
Keywords: | Resolution: fixed
Has patch: 1 | Triage Stage: Ready for
Needs tests: 0 | checkin
Easy pickings: 0 | Needs documentation: 0
| Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/17005#comment:14>

Reply all
Reply to author
Forward
0 new messages