Code

Opened 7 years ago

Closed 6 years ago

Last modified 6 years ago

#4626 closed (wontfix)

pass the context to extra_context functions

Reported by: bennydaon@… Owned by: nobody
Component: Generic views Version: master
Severity: Keywords:
Cc: sciyoshi@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by mtredinnick)

I found it helpful for extra_context functions to have access to the context.
The change is in line 80:

<        if callable(value):
<            c[key] = value()

>        if callable(value):
>            c[key] = value(c)

I'm not sure who decides on whether it should go to the version - it does changes the API

Attachments (0)

Change History (7)

comment:1 Changed 7 years ago by mtredinnick

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

(Fixed description formatting.)

What file are you trying to change here? What is an example use-case?

comment:2 Changed 7 years ago by bennydaon@…

This change is to list_detail.py and I use it for the function display_pages:

def display_pages (c):
        ''' return an HTML string that renders: "1 2 3 4 ..." and has a link for all pages except the current. '''
        return ' '.join([p==c['page'] and '%s&nbsp;' % p or '<a href="/foo/page/%s">%s&nbsp;</a>' % p for p in range(1, c['pages']+1)])

comment:3 Changed 7 years ago by SmileyChris

  • Resolution set to wontfix
  • Status changed from new to closed

Sounds like you should be doing this as a template tag, not an extra_context function. Also taking into consideration the fact that this would break everyone's current functionality, I'll call it and say wontfix.

comment:4 Changed 6 years ago by sciyoshi@…

  • Cc sciyoshi@… added
  • Resolution wontfix deleted
  • Status changed from closed to reopened

I'd like to ask for this again. I have the following situation: two models, Category and Post, where each Post has a Category. I want object_detail for Categories to display a list of the category's posts by {% extend %}ing 'post_list.html'. Unfortunately, doing this any other way (i.e., a context processor or a list_detail view on the Post model) are either ugly or require multiple lookups for the category. With this, everything is much easier:

(r'/categories/(?P<slug>.+)/$', object_detail, dict(
    queryset=Category.objects.all,
    template_object_name='category',
    slug_field='slug',
    extra_context=dict(
        post_list=lambda context: context['category'].get_posts(),
    ),
))

Note that you can add this functionality without breaking anything:

if callable(value):
    try:
        c[key] = value()
    except TypeError:
        c[key] = value(c)

comment:5 Changed 6 years ago by mtredinnick

  • Resolution set to wontfix
  • Status changed from reopened to closed

Please do not reopen tickets that have been marked as wontfix. We ask this for a reason: so that we don't continually have requests reopened just because people don't agree with them. If you are unhappy with the resolution, the contributing.txt document (also available in our documentation section online) explains what the next step is. Read the section in there about ticket statuses.

I am not commenting on your request, since following the normal procedure is important here.

comment:6 Changed 6 years ago by daonb <bennydaon@…>

I've opened this ticket, but fully agree with the decision to close it as 'wontfix'. If you need to access any of the context parameters, you're better of using a customer templatetag. I found the use of templatetag make reuse easier and the code more readable.

Templatetags are quite easy to write and can accept any number of parameters. If I understand your issue, you need a template tag that will return a post list based on the 'category' parameter:

{% get_posts for category as post_list %}

You can find an explanation on how to write your own templatetags here.

Hope it helps.

comment:7 Changed 6 years ago by Samuel Cormier-Iijima <sciyoshi@…>

The problem I was having was that context variables set in the child template (through a template tag) didn't seem to be accessible from the parent. The problem was that I wasn't setting the variable inside a block tag, so it wasn't getting pushed onto the parent's context. Anyways, although I think it would be nice for small things like this to not have to write a whole template tag, I respect the developers' decision to wontfix this.

Thanks,
Samuel

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.