Hacker News new | past | comments | ask | show | jobs | submit login
Effective Django (effectivedjango.com)
227 points by equilibrium on Mar 30, 2013 | hide | past | web | favorite | 42 comments

I'd recommend a section on 'Function Based Views' in there as well. I've personally found the CBV-based approach is sometimes good but in most cases I still prefer the FBV approach. You see it often enough that I think it is worth going over in detail.

Is there any reason to still use them though? You could always just define a `get`, `post`, (etc etc) method and have pretty much the same effect of a functional view but with the added niceness of what CBV's provide (mainly inheriting some other view with a lot of boiler plate)

http://lukeplant.me.uk/blog/posts/djangos-cbvs-were-a-mistak... http://lukeplant.me.uk/blog/posts/class-based-views-and-dry-...

IMO, CBVs are good, but for many cases FBV are simpler to code and easier to understand.

Defining a get method on a class is just as simple and allows for future expansion.

…future expansion that I will probably never use. And I disagree that it's just as simple. I think that CBV's encourage me (and my team, because I'm not working in a vacuum) to think about the problem all wrong. You may like thinking of the problem that way but for me it's like trying to think of a song while another song is playing. I usually think of Django views as a function, not a method on a class.

Django's FBV's are far from perfect and I think they really could benefit from some small changes to the way things are done, but I agree with the above links that CBV's were a step in the wrong direction.

I'll have to just disagree with you here. I don't find the following any more difficult then a functional view

class MyView(View): def get(self): return stuff

It's one more line, and is much more explicit in what it handles.

“Sometimes, the elegant implementation is just a function. Not a method. Not a class. Not a framework. Just a function.” — John Carmack

I recently ran into issues which I could only explain as CBV not being thread safe. self.request.user would not remain consistent when a view needed to save multiple models. I haven't had time to fully debug, I only know that frantically patching to not rely on properties, rather arguments, fixed it. We're also running gunicorn on Heroku, not sure yet if that's related. Either way, something to consider.

You have no flow control in CBVs. Why code some methods and stuff when you can put it in a simple function?

If you want to see the video of the guy who wrote Effective Django giving it as a tutorial, here you go: http://pyvideo.org/video/1717/effective-django-0

This is wonderful! I feel like the django documentation, combined with 10,000 stack overflow questions is the wrong way to get started, and yet, that's how it was for me. I will use this all the time for tips and pointers. Thanks!!

Fantastic so far, easy to follow.

I've come to a snag at "Writing a Test" (http://effectivedjango.com/tutorial/models.html#writing-a-te... ) though, it's not clear which file I should be adding the test class to.

From following the tutorial I would think models.py but from browsing the file structure I would expect test.py

I don't know if this is the absolute best way but Two Scoops of Django recommends having a tests folder in each app. So tests for example_app/views.py are placed in example_app/tests/views.py.

Yup, it's tests.py; I'm updating the doc now to be more explicit, thanks for calling that out!

yes, you put it in the tests.py file in your app directory. That way you can just run "python manage.py test myapp" and every test in myapp/tests.py will be executed.

Looks fantastic! Is there a way to get the whole thing in ebook format?

I just built an EPub version from Sphinx; you can find it at http://effectivedjango.com/epub/EffectiveDjango.epub. There are links from the front page to the EPub and PDF versions.

Thanks a lot!!!

Check the Makefile. It's probably already doable.



This is awesome. I haven't touched Django even though I develop in Python on a daily basis... been meaning to but hadn't found a reason to. I've bookmarked this for when I finally get the itch.

Btw, there are some weird formatting issues in the tutorial (underscores after links?).

It's possible those are bad link targets in the ReStructured Text source, I'll take a look. Thanks for calling that out!

I dont see any issue with the formatting. I'm on Chrome 26.0.1410.43 on a Mac OSX 10.8.

Not a single mention of AJAX.

Writing the server side of an AJAXy search was pretty straightforward. In your view function, Instead of rendering a template, you call json.dumps() (or the equivalent in XML), and set the content type of the HTTP response (i think "application/json"). Django won't generate your client side JavaScript for you (nor should it IMO), so use jQuery or whatever you want.

good point! django doesnt have any built in http resposnes or decorators for returning json (as far as i know anyways). I would recommend looking at these two options:



Why is this an issue with an article about writing effective django?

Many web apps/services now use at least a little AJAX. Certain things in a framework can make it easier. Some discussion of how AJAX techniques, up through 'single-page apps', can use a DJango back-end would be nice. (Maybe not absolutely necessary... but nice.)

Return JSON. The end.

Then, a bit of docs about the JSON apis to use, and caveats/gotchas in using them with Django Models would be helpful. The popular Django extension TastyPie could be discussed, as well as URL schemes compatible with common client-side libraries.

Similarly, ways to mimic single-page-app URLs from the server side, for first-load speed or SEO benefits, could be discussed.

What is a JSON api? Anyway, your entire wish list is out of the scope of the document. If you want to know how to build a hypermedia API (which I think is what you meant by json api) what does that have to do with "effective Django"? That would be "building effective hypermedia APIs using Django".

A JSON API is the methods & options that are available for (de)serializing your native Python or Django objects into useful JSON.

Perhaps there's a more narrow 'scope' defined for this project, but simply from the title, it seems an 'effective' Django developer would appreciate guidance for the best ways to do AJAXy things (including, especially, RESTy web services for backing AJAXy front-ends).

AJAX is nothing more than sending and receiving data from the server to the client. That's what views do, and they are documented.

There is also an is_ajax() test in Django (not that useful) and various apps that provide REST interfaces that you'd probably want to use for bigger AJAX-y apps.

you can use Django-rest-framework ,it's good.

The folks behind this project know Django really well - I've recommended this to people a number of times.

This is awesome, I've been starting to get into Django over the last few weekends, and this is a godsend!

This is nice, but I'd recommend two scoops of django over this.

Two Scoops is solid, but it's also a $14 ebook, whereas Effective Django is free. Not really apples to apples.

This frustrates me. Three hours of time to watch a video, if you charge $50/hr means you spend $150 opportunity cost of time on the free version. Assuming the time to consume both is equal, it gives an equivalent cost of $150 vs $164, or put another way, the difference in cost is a rounding error.

It seems to me the only durable advantage to the free version is that if it isn't working out you can abandon it after 15 minutes at a fraction of the cost. If there were a credible way to sell knowledge products "satisfaction guaranteed" I'd think that people would be a lot less sensitive to the difference between free and $14.

Interesting, I hadn't heard about this. Here is a link for the lazy: https://django.2scoops.org/

considering that I already know python, if I wanted to make a good looking personal gtd type webapp, am I better off learning django or should I rather learn node.js?

I worked with Nathan (guy behind Effective Django) at Eventbrite for a few years and highly recommend him as being a very informed python and django expert.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact