I was asked to write about “correct” django project’s structure at least twice. To be honest, I’m not very enthusiastic about this because there is no silver bullet in this case. Structure of the project must evolve with project and must be determined by its needs. But I can suggest some techniques that may help (I also must note that most of them may be used somewhere already).
Django is Python code. Structure your code as you need. When models.py
is too long, split it (but don’t forget about app_label
). Some people tend to split out Manager subclasses into managers.py
(or managers
package) even if it’s very likely that it will be the only Manager in file, but I’d not recommend you to do that unless you have few large Managers.
Though render_to
decorator is rather popular now, I suggest to name it as_html
. So now you can have decorator family: as_xml
, as_json
, etc. Use feed
and confirm
decorators (wisely; maybe I’ll publish them someday :) Let’s assume you have a view:
@feed
@as_html('questions/list.html')
def questions_list(request):
questions = Question.active.last(50)
return {'objects': questions}
Now /questions/
URL will show rendered template and /questions/rss/
will show RSS 2.01 feed. You can use confirm
decorator like this:
@confirm(messages={
'confirmation': 'Do you really want to delete this question?',
'success': 'Question deleted',
'error': 'You can\'t delete this question'},
#action=lambda obj: obj.delete(), # this is assumed by default
#cancel=lambda obj: obj.get_url_path(), # this is assumed by default
#authorize=lambda obj, user: obj.can_be_deleted_by(user), # this is assumed by default
redirect_to=lambda obj: reverse('questions_list'))
def delete_question(request, id):
question = get_object_or_404(Question, id=id)
return {'obj': question}
It works like lorien’s confirmation processor, but it’s, you know, decorator :) Also few pagination decorators exist.
For futher reading I’d recommend you Django Project Conventions, revisited by Zachary Voase, Django Best Practices by Lincoln Loop and then search for “django project structure” in your favourite search engine.
This blog is about things I encounter while doing web and non-web software development.