[Django] #18651: Assignment tags should allow optional assignments

31 views
Skip to first unread message

Django

unread,
Jul 20, 2012, 12:42:46 AM7/20/12
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
---------------------------------+--------------------
Reporter: mitar | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: 1.4
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------
Assignment tags should allow optional assignments, so that if `as` part is
missing, they output the result. Otherwise store it into variable.

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

Django

unread,
Aug 25, 2012, 6:57:07 AM8/25/12
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
---------------------------------+--------------------------------------
Reporter: mitar | Owner: nobody
Type: New feature | Status: closed
Component: Template system | Version: 1.4
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by msopacua):

* status: new => closed
* cc: m.r.sopacua@… (added)
* needs_better_patch: => 0
* needs_tests: => 1
* needs_docs: => 0
* resolution: => needsinfo


Comment:

Why? What is your use case? What is the problem you cannot solve?

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

Django

unread,
Aug 25, 2012, 9:13:48 PM8/25/12
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
---------------------------------+--------------------------------------
Reporter: mitar | Owner: nobody
Type: New feature | Status: closed
Component: Template system | Version: 1.4
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

Comment (by mitar):

To have a simple tag made with a decorator which could serve both as a
simple tag or as a assignment tag. Currently you can have only simple tag
OR assignment tag, not both.

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

Django

unread,
Dec 13, 2012, 7:21:38 PM12/13/12
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
---------------------------------+--------------------------------------
Reporter: mitar | Owner: nobody
Type: New feature | Status: closed

Component: Template system | Version: 1.4
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

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

Comment (by tim_heap):

I can think of two main use-cases for this, and two problems it solves.
One of these use-cases has already been encountered in the built in tags,
as documented below, so the problem it solves is real.

Firstly, when a sensible default name exists for a tag, but you may want
to change it. Consider a tag `{% get_comments_for object %}`, which find
all comments against the object passed in, and assigns the result to the
`comments` variable in the template. If you have two objects on the page,
and want to get the comments from both, the names would collide. In this
case, you could use `{% get_comments_for foo as foo_comments %}` and `{%
get_comments_for bar as bar_comments %}`.

The second use case is assigning or outputting the result. This is already
used by the built in `{% url %}` tag. You can use `{% url foo %}` to print
the URL directly, or `{% url foo as foo_url %}` to assign it to the `foo`
variable in the template.

This could be implemented as either two new decorators:

{{{
@register.assignment_tag_with_default(default='comments')
def get_comments_for(object):
pass

@register.optional_assignment_tag()
def url(name, *args, **kwargs):
pass
}}}

or as options on the existing decorator

{{{
@register.assignment_tag(default_name='comments')
def get_comments_for(object):
pass

@register.assignment_tag(optional_assignment=True)
def url(name, *args, **kwargs):
pass
}}}

In the second case of extending the current decorator, `default_name` and
`optional_assignment` would have to be mutually exclusive.

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

Django

unread,
Dec 13, 2012, 11:45:43 PM12/13/12
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
---------------------------------+--------------------------------------
Reporter: mitar | Owner: nobody
Type: New feature | Status: reopened

Component: Template system | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

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

* cc: tim_heap (added)
* needs_docs: 0 => 1
* has_patch: 0 => 1
* status: closed => reopened
* resolution: needsinfo =>


Comment:

I've implemented this and published it on github. I went with the second
option of extending the existing `assignment_tag` decorator. Tests have
been written to make sure it works. You can find my branch here:
https://github.com/maelstrom/django/commits/ticket-18651

I have not yet written documentation for the changes.

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

Django

unread,
Dec 16, 2012, 7:22:53 PM12/16/12
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
---------------------------------+--------------------------------------
Reporter: mitar | Owner: nobody
Type: New feature | Status: reopened

Component: Template system | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

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

Comment (by tim_heap):

After discussing with people in #django-dev, the following was proposed:

* Merge these changes in to `simple_tag`.
* Start `assignment_tag` on the deprecation path.

For example:

{{{
@register.simple_tag()
def plain_output():
"""
Use as `{% plain_output %}`. Prints 'bar'
"""
return 'bar'


@register.simple_tag(assignable=True)
def optional_assignment():
"""
Use as either:

* `{% optional_assignment %}` - prints 'bar'
* `{% optional_assignment as foo %}` - assigns 'bar' to 'foo'
"""
return 'bar'


@register.simple_tag(assignable=True, default_name='foo')
def default_assignment():
"""
Use as either:

* `{% default_assignment %}` - assigns 'bar' to 'foo'
* `{% default_assignment as foo %}` - assigns 'bar' to 'foo'
"""
return 'bar'
}}}

This has the down side of being unable to force assignment, as you
currently can with `assignment_tag`. I do not see this being a problem
though, as I can not think of a reasonable example where outputting a
variable is so harmful that the possibility of it happening should be
guarded against explicitly in the tag. If you really need this case, write
a completely custom tag.

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

Django

unread,
Dec 19, 2012, 7:17:04 PM12/19/12
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
---------------------------------+--------------------------------------
Reporter: mitar | Owner: nobody
Type: New feature | Status: reopened

Component: Template system | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

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

Comment (by tim_heap):

I have made the modifications to `simple_tag` now, so that it supports
`can_assign` and `default_name`. The code is on github:
https://github.com/maelstrom/django/compare/django:master...ticket-18651-v2

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

Django

unread,
Dec 20, 2012, 1:30:05 AM12/20/12
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
-------------------------------------+-------------------------------------
Reporter: mitar | Owner: nobody
Type: New feature | Status: reopened

Component: Template system | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Design
Has patch: 1 | decision needed
Needs tests: 1 | Needs documentation: 1
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by russellm):

* stage: Unreviewed => Design decision needed


Comment:

I'll accept the broad idea, but the specifics of the API design still need
to be clarified.

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

Django

unread,
Jan 14, 2013, 9:41:00 PM1/14/13
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
-------------------------------------+-------------------------------------
Reporter: mitar | Owner: nobody
Type: New feature | Status: reopened

Component: Template system | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Design
Has patch: 1 | decision needed
Needs tests: 1 | Needs documentation: 1
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by anonymous):

A write up of the proposals can be found here:
https://gist.github.com/4534963

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

Django

unread,
Mar 23, 2013, 5:57:49 PM3/23/13
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
---------------------------------+------------------------------------

Reporter: mitar | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

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

* stage: Design decision needed => Accepted


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

Django

unread,
Aug 14, 2013, 11:28:48 AM8/14/13
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
-------------------------------------+-------------------------------------
Reporter: mitar | Owner:
Type: New feature | jonathanslenders
Component: Template system | Status: assigned
Severity: Normal | Version: 1.4
Keywords: | Resolution:
Has patch: 1 | Triage Stage: Accepted

Needs tests: 1 | Needs documentation: 1
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jonathanslenders):

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


Comment:

I think I can do this. It fits nicely in my work on ticket #20434 which is
a huge refactoring.

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

Django

unread,
Aug 27, 2013, 4:00:36 AM8/27/13
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
---------------------------------+------------------------------------
Reporter: mitar | Owner:

Type: New feature | Status: new
Component: Template system | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

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

* status: assigned => new
* owner: jonathanslenders =>


Comment:

Deassigned, because I'm lacking some time right now. So, anyone feel free
to work on this ticket. Otherwise, I'll take it again in a few weeks.

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

Django

unread,
Feb 2, 2015, 3:32:03 PM2/2/15
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
---------------------------------+------------------------------------
Reporter: mitar | Owner:

Type: New feature | Status: new
Component: Template system | Version: 1.4
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 prestontimmons):

* needs_docs: 1 => 0
* needs_tests: 1 => 0


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

Django

unread,
Feb 2, 2015, 3:32:47 PM2/2/15
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
---------------------------------+------------------------------------
Reporter: mitar | Owner:

Type: New feature | Status: new
Component: Template system | Version: 1.4
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 prestontimmons):

I added a pull request here:

https://github.com/django/django/pull/4034

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

Django

unread,
Feb 3, 2015, 10:45:40 AM2/3/15
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
-------------------------------------+-------------------------------------
Reporter: mitar | Owner: Tim
| Graham <timograham@…>

Type: New feature | Status: closed
Component: Template system | Version: 1.4
Severity: Normal | Resolution: fixed

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 Tim Graham <timograham@…>):

* owner: => Tim Graham <timograham@…>


* status: new => closed

* resolution: => fixed


Comment:

In [changeset:"cd4282816db9164791cd0ac97a3dc329ad92c522"]:
{{{
#!CommitTicketReference repository=""
revision="cd4282816db9164791cd0ac97a3dc329ad92c522"
Fixed #18651 -- Enabled optional assignments for simple_tag().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/18651#comment:15>

Django

unread,
Jan 17, 2017, 10:09:50 PM1/17/17
to django-...@googlegroups.com
#18651: Assignment tags should allow optional assignments
-------------------------------------+-------------------------------------
Reporter: Mitar | Owner: Tim

| Graham <timograham@…>
Type: New feature | Status: closed
Component: Template system | Version: 1.4
Severity: Normal | Resolution: fixed
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 Tim Graham <timograham@…>):

In [changeset:"f032bbc8b107ab9274542f8d233fc88aa1c6e04d" f032bbc]:
{{{
#!CommitTicketReference repository=""
revision="f032bbc8b107ab9274542f8d233fc88aa1c6e04d"
Refs #18651 -- Removed assignment_tag per deprecation timeline.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/18651#comment:16>

Reply all
Reply to author
Forward
0 new messages