Opened 15 years ago
Closed 13 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 )
Attachments (1)
Change History (10)
comment:1 by , 15 years ago
| Description: | modified (diff) | 
|---|
comment:2 by , 15 years ago
| Component: | Documentation → Generic views | 
|---|
comment:3 by , 15 years ago
| Easy pickings: | set | 
|---|
comment:4 by , 14 years ago
| Owner: | changed from to | 
|---|---|
| UI/UX: | unset | 
by , 14 years ago
| Attachment: | patch_15906.diff added | 
|---|
comment:5 by , 14 years ago
| Has patch: | set | 
|---|
comment:6 by , 14 years ago
| Component: | Generic views → Documentation | 
|---|---|
| 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 , 13 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 , 13 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 , 13 years ago
| Resolution: | → fixed | 
|---|---|
| Status: | new → closed | 
A new section on how to use head() in a generic view.