Django templating is another topic that would be a good main subject for an upcoming meeting. I think Django add-ons would be another good topic.
Based on my experience in the past few years, I personally use class-based views exclusively. This could, by itself, be yet another meeting topic. Maybe it would be fun to have a function vs. class-based views meeting where we could have some people present their cases in a debate style discussion.
For me, the reason that I use class-based views exclusively is really due to extensibility. In my older code I have over and over again the same function-based view that looks like this:
# this is all from memory, so don't judge my errors
def myview(request):
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
form.save()
return http.HttpRedirect("/somewhere")
else:
return http.HttpResponse("some template", context=RequestContext({'errors': form.errors, 'form': form}))
else:
return http.HttpResponse("some template", context=RequestContext({'form': form}))
I see a huge problem with this. It is not extensible at all. I cannot write another view identical to this one in every way except that it uses a different form class, or that passes one additional piece of data to the request context, or that does something special when the form is saved, or redirect to a different url or does something special for an HTTP PUT, PATCH, DELETE etc. A class based view breaks all of this up so you would have something like:
class MyView:
template_name = "some template"
form_class = MyForm
redirect_url = "/somewhere"
def form_valid(self, form):
"special hook to save the form the way you want"
def form_invalid(self, form):
"special hook to do something when the form is not valid"
def get_context_data(self):
"special hook to add what you want to the template's context"
def get(self):
"this happens for an HTTP GET request"
def put(self):
"this happens for an HTTP PUT request"
def patch(self):
"this happens for an HTTP PATCH request"
The class based view approach is completely extensible. You can easily re-use views from 3rd party libraries without having to copy and paste a giant nightmarish function-based view's code and tweaking the one line that you want to behave differently.
Here is a real example from real world code. In the
Django admin's login view there is a check in there that calls is_safe_url. Our Django applications use single sign-on, which means that you don't necessarily authenticate on the same domain as the application (we verify everything behind the scenes). That means that this one small line of code makes it impossible to use the Django provided login view at all. If there would have been a way for us to override a method in a subclass to change the behavior of that one line of code, or even some subset of that function we wouldn't have had to re-roll that exact view for our applications.
While I do agree that class-based views seem to introduce more LOC in examples like this, I found that a majority of our class based views are really no code, just configuration for example:
class MyView:
template_name = "x"
form_class = "x"
model_class = "x"
This really just isn't possible using function based views. Views should be logic-less right? They should just wire things together and delegate to the appropriate objects that do all of the real work. Class-based views seem like the right tool for the job, to me.
I would like to have a meeting about Django views and Django templates and Django add-ons, so if you're still reading this massive post, please chime in and let me know if you would be interested in speaking about one (or more) of these topics.
-Matt