as_view: a dirty trick
Here's one of my sleaziest tricks for Django:
def as_view(cls):
view = cls.as_view()
view.cls = cls
return view
And here's how you use it:
@as_view
class SomeView(ListView):
...
What does this do for you? It gives you the incredible comfort of making your URLs much nicer:
path('some/', views.SomeView),
# instead of this horrific thing:
path('some/', views.WorseView.as_view()),
Is that not absolutely nice? Yes! Yes it is!
You're wondering what view.cls
is supposed to mean? Well, first of all, you can add arbitary attributes to python functions. Python functions are just instances with a .__call__
methodMost of the times, anyway. There are other different cases.. So add attributes as much as you want to.
Adding the .cls
attribute gives us access to the actual class that this view function was derived from. You might want this sometimes to derive other classes from it. Like this:
@as_view
class SomeView(ListView):
...
class SomeOtherView(SomeView.cls):
...
And that's it. I use it in every Django project and feel very smug/sleazy about having much nicer URL patterns.
A detour: decorators
I like decorators. They're useful. But they can be hard to understand.
At its most basic, a decorator is a mechanical translation of this:
@decorator
def some_func():
...
into this:
def some_func():
...
some_func = decorator(some_func)
That's all there is to it. You replace a function or class with that same function/class that has been passed through a function. The hard part is what to do with it. And more complex use-cases, obviously.
If you need more complex decorators, I suggest you have a look at wrapt, which will take away most of the pains of writing them.
License
The most basic form of the function is:
def as_view(cls):
return cls.as_view()
I don't think that a two-line function that is kind-of obvious has enough Schaffenshöhe to warrant copyright. However, to be on the safe side, you may choose to receive the software and text of this article under the terms of the Blue Oak Model License 1.0.0 as written here:
Blue Oak Model License
Version 1.0.0
Purpose
This license gives everyone as much permission to work with this software as possible, while protecting contributors from liability.
Acceptance
In order to receive this license, you must agree to its rules. The rules of this license are both obligations under that agreement and conditions to your license. You must not do anything with this software that triggers a rule that you cannot or will not follow.
Copyright
Each contributor licenses you to do everything with this software that would otherwise infringe that contributor's copyright in it.
Notices
You must ensure that everyone who gets a copy of any part of this software from you, with or without changes, also gets the text of this license or a link to https://blueoakcouncil.org/license/1.0.0.
Excuse
If anyone notifies you in writing that you have not complied with Notices, you can keep your license by taking all practical steps to comply within 30 days after the notice. If you do not do so, your license ends immediately.
Patent
Each contributor licenses you to do everything with this software that would otherwise infringe any patent claims they can license or become able to license.
Reliability
No contributor can revoke this license.
No Liability
As far as the law allows, this software comes as is, without any warranty or condition, and no contributor will be liable to anyone for any damages related to this software or this license, under any kind of legal claim.