Opened 13 years ago

Closed 12 years ago

#15906 closed Bug (fixed)

New head method needs documentation

Reported by: Jannis Leidel Owned by: Zbigniew Siciarz
Component: Documentation Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: yes UI/UX: no

Description (last modified by Jannis Leidel)

The head method added in r16095 & r16105 needs documentation.

Attachments (1)

patch_15906.diff (2.9 KB ) - added by Zbigniew Siciarz 12 years ago.
A new section on how to use head() in a generic view.

Download all attachments as: .zip

Change History (10)

comment:1 by Jannis Leidel, 13 years ago

Description: modified (diff)

comment:2 by Jannis Leidel, 13 years ago

Component: DocumentationGeneric views

comment:3 by Alex Gaynor, 13 years ago

Easy pickings: set

comment:4 by Zbigniew Siciarz, 12 years ago

Owner: changed from nobody to Zbigniew Siciarz
UI/UX: unset

by Zbigniew Siciarz, 12 years ago

Attachment: patch_15906.diff added

A new section on how to use head() in a generic view.

comment:5 by Zbigniew Siciarz, 12 years ago

Has patch: set

comment:6 by Jannis Leidel, 12 years ago

Component: Generic viewsDocumentation
Patch needs improvement: set

This looks good in principle, but I wonder if the example should use the @last_modified decorator instead: https://docs.djangoproject.com/en/dev/topics/conditional-view-processing/#shortcuts-for-only-computing-one-value

comment:7 by Tim Graham, 12 years ago

@jezdez, I've spent a while looking at it, but it's unclear to me how to use the @last_modified decorator inside the class-based view. The tricky part is how to pass the "last_modified_func". I imagine it might be something like the following:

class EntryListView(ListView):
    model = Entry

    def latest_entry(self, request):
        return self.get_queryset().latest('published').published.strftime('%a, %d %b %Y %H:%M:%S GMT')

    @method_decorator(last_modified(latest_entry))
    def head(self, *args, **kwargs):
        return HttpResponse('')

Any insight?

comment:8 by Tim Graham, 12 years ago

Consulted with Malcolm on IRC -- didn't seem like using last_modified is viable so going to commit this as is.

malcolmt: the code in that comment looks essentially correct. Have you tried it and does it work?
malcolmt: That's what method_decorator() is for.
me: the issue with the code as I posted it is that latest_entry needs to be called with "self" (exception is: latest_entry() takes exactly 2 arguments (1 given))
malcolmt: oooh.
malcolmt: That old chestnut: functions != methods. again. :-(
malcolmt: and you need "self" in that function, so you can't make it @classmethod.
malcolmt: bummer :(
me: do you think the answer is "it's not possible" then?
malcolmt: Probably "not possible yet". It *might* be worth changing the signature on last_modified and etag to make them able to handle methods. I'm waiting for carljm to provide a consult.
malcolmt: It's almost unnecessarily fiddly sometimes in Python to make things that work with both functions and methods, it seems.
me: sounds good, thanks for the assist. I'll go ahead and commit the doc and we can revise it to use last_modified if that works out
malcolmt: I think you're using last_modified poorly in that example, since it's meant to be computed based on the request and the URL params. "self" really has no role there except as a way to avoid having to pull out the object again.
malcolmt: What's really need is a last_modified_for_methods() function that understands how to work with class methods.
malcolmt: I don't think there's an easy soln in the current setup. I also have trouble caring much about class-based views, so it's not something I'm likely to spend time on fixing. Sorry.

comment:9 by Tim Graham <timograham@…>, 12 years ago

Resolution: fixed
Status: newclosed

In [518c58296667ffc36f90b873eb97aa99fa00eb1e]:

Fixed #15906 - Documented HEAD method in CBVs; thanks zsiciarz for the patch.

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