10 Jan 2010

Nice & concise: Top 10 tips to a new django developer : Dpeepul Blog

Rama Vadakattu has some good pointers for people just starting out with Django. Even if you are an old pro who was around during the first #import it still doesn't hurt to refresh your dev memory from time to time on Django best-practices.

Top 10 tips to a new django developer

 

1.Don’t put project name  in the imports

For example suppose say you have an app “xyz” in  the project say “project3” then don’t say

1: from project3.xyz.models import Author

It ties the project name to your app which has several disadvantages like

1.You can’t easily reuse the app.

2.In future if you want to change the project name it becomes difficult.

Instead do this.

1: from xyz.models import Author

You can write the above statement as the django project is there on the  python path.

2.Don’t hard code MEDIA_ROOT and TEMPLATE_DIRS

In settings.py don’t write MEDIA_ROOT and TEMPLATE_DIRS as below

1: TEMPLATE_DIRS = ( "/home/html/project/templates",)
2: MEDIA_ROOT = "/home/html/project/appmedia/"

The above will cause problem when you move your project to deployment servers (changing from one server to another server) , changing from one OS to another OS  as you need to take of proper directory structure.

By following the below technique  you can easily avoid the above problems

1: SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
2: MEDIA_ROOT = os.path.join(SITE_ROOT, 'appmedia')
3: TEMPLATE_DIRS = ( os.path.join(SITE_ROOT, 'templates'),)

This technique which  i have learned from Rob Hudson and more details you can find at the below link:

http://rob.cogit8.org/blog/2009/May/05/django-and-relativity-updated/

3.Don’t hard code static files in your templates.

What i mean by above is :

When you want to link static files like (java script  files,css files,images) don’t do the below

( Let us assume that MEDIA_URL is “/appmedia/” )

1: <link rel="stylesheet" type="text/css" href="/appmedia/wysiwyg/jHtmlArea.css" />
2:  <script type="text/javascript" src="/appmedia/wysiwyg/jHtmlArea.js"></script>
3:  <script type="text/javascript" src="/appmedia/scripts/editor.js"></script>
4:  <script type="text/javascript" src="/appmedia/scripts/comments.js"></script>
5:  <script type="text/javascript" src="/appmedia/scripts/voting.js"></script>

The problem with above approach  is say, suppose if you want to server static content via another server (or) with amazon S3 from now let us  say from (http://cdn.xyz.com/)  then you need to rewrite every template replacing  “/appmedia/” with   http://cdn.xyz.com Ah………… so tedious.

You can eliminate the above tedious process by following the simple technique.(i.e) Use the  context variable {{MEDIA_URL}} instead of hard coding as “/appmedia/” as below

1: <link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}wysiwyg/jHtmlArea.css" />
2: <script type="text/javascript" src="{{ MEDIA_URL }}wysiwyg/jHtmlArea.js"></script>
3: <script type="text/javascript" src="{{ MEDIA_URL }}scripts/editor.js"></script>
4:
5: <script type="text/javascript" src="{{ MEDIA_URL }}scripts/comments.js"></script>
6: <script type="text/javascript" src="{{ MEDIA_URL }}scripts/voting.js"></script>

Using the above approach when you change the  MEDIA_URL from “/appmedia/” to “http://cdn.xyz.com/”.It is  automatically reflected in all templates.No tedious process to changing all the templates.

How do we get the Context variable?

Very simple.You can pass various useful context variables (like user,requested page)  to templates by adding  RequestContext(request)  to render_to_response as below

1: return render_to_response("my_app/my_template.html", {'some_var': 'foo'},
2:                            context_instance=RequestContext(request))
More details:

http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/

via blog.dpeepul.com

 

See all 10 tips and more at blog.dpeepul.com