Django questions and answers with a Swedish guy

Manolo Guerrero, that funny talking Mexican dude who happens to be a jQuery god and excellent friend of mine, introduced me to Andreas Krohn, a great pal from Sweden, evil mind behind WebHostNinja, with whom I've developed a nice Google App Engine and Django based Facebook application that will go live soon and I'll discuss later.

Andreas is a highly energetic online entrepreneur and developer who's considering Django for his next project and asked me a few questions about it. These were very good questions that many may have asked before and I thought my answers could help others getting used to the Django way of thinking.

Andreas:

Alexis, I've spent some time on reading up on Django after your recommendation to use it for the new project. There is really a lot of good documentation online, especially djangoproject.com and djangobook.com, (Ventanazul also helps!), but there are a few questions I haven't found answers to and I wonder if you can point me to some site or other resource that can help me there:

  • How do I use different applications in the same project? Some apps can be kept completely separate, but some need to share templates or functionality. I want to do this without creating complicated interdependencies and spaguetti code.
  • What are the best practices for Django development?

Thanks!

Alexis:

Hey, those are very good questions. I don't consider myself a Django ninja yet but I think that after developing a few projects I have some solid ideas on the best way to approach Django development (note to readers: please correct me where I'm wrong).

As you already know every Django project has a settings.py file where you can list the apps that it uses. In many simple cases apps directories live inside your project directory like this:

/project
/app1
/app2

but that doesn't need to be the case everytime. You can have your apps anywhere in the Python path so you can use one app from many projects, James explains it better here: reusable Django apps.

Templates can be included within the apps, I think using a templates directory in the app directory is the standard, and then those templates can be overriden by the templates for each project, defined in its settings.py. An example is how the admin templates are overriden in part 2 of the tutorial.

One important thing I've found with Django: whenever you feel like you're repeating yourself or creating spaguetti code it's almost a sure thing that Django includes a way to make it simpler and cleaner.

Best practices for Django? I've identified a bunch:

  1. Don't repeat yourself.
  2. Keep everything independent, loose coupling is a must.
  3. Templates must be simple, never do logic inside templates, except for some simple loops and conditions. Use views for more complex decisions and leave templates to take care of presentation.
  4. Before writing new code check if somebody else already wrote something to do what you need to. In some cases, like the comments apps, you just need to plug and play, in others you can reuse and improve code.
  5. Never hardcode urls, use a well designed URLConf, with named patterns, reverse in your views and {% url %} in your templates.
  6. Finally, the Django users group is great, lots of smart and very helpful guys over there. I always search for answers there first and in most cases somebody already asked something I needed to know, if not, the time between posting your questions and getting useful replies can be just a few hours or even minutes.

Resources

Of course this is not everything about Django under the Sun, if you find anything else let me know.

Andreas:

Thanks for your long answer! It is definitely blog worthy.

There is one thing I still do not really understand though.... overriding templates and making things all look good is one thing. A more complicated thing is how to actually use one app from another. Say I have one app that handles user sessions and authentication, and another that handles blog comments. Now I just want logged in authorized users to leave comments, so I need to use the authentication app to handle that for the comment app. Parts of this could be done in a template, but say (for this example) that you need to do more than that. Would you then change the comments app and have the authentication app as a dependency for the comment app? Would you write a third customized app that uses the auth and comment apps to make them work nicely together? If this is a confusing question it is because I am confused. For my project I do need to use lots of apps and I want to use them correctly from the beginning so I don't hack something together that won't work in a few months time.

Again, thanks for helping out!

Alexis:

Yeah, I think that writing a third app that uses both the authentication and comments app would be the recommended route. That's similar to what we do in Drupal development. I usually start a Drupal project writing a site-specific module that requires and uses all other contributed modules.

For Django that would be writing a project specific app to make sure that all the other apps run well together.

In your case that third app would be the only connection between auth and comments, ideally the two of them don't even need to know the other one exists.

That's it

Thank you Andreas for the questions and for helping to share the knowledge about Django.

As usual, all comments are welcome.

Trackback URL for this post:

http://ventanazul.com/webzine/trackback/129

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h1> <h2> <h3> <h4>
  • Lines and paragraphs break automatically.

More information about formatting options