Here note summarize the ten points that can help you better lean Django,reduce errors and avoid detours,it is worth a look.
Here note summarize the ten points that can help you better lean Django,reduce errors and avoid detours,it is worth a look.
1.Don’t include project’s name in reference code.
If you create a project’s name “project”, include a application’s name “app”, it’s no good like this.
from project.app.models import Author
the disadvantages is the project and application are tight coupling. cannot reuse application. it’s very difficulty to change new project.
So it’s the way for recommended.
from app.models import Author
In addition, it’s need set the project path in the “PYTHONPATH”.
2.Don’t hardcoed “MEDIA_ROOT” and “TEMPLATE_DIRS”
Don’t use the following code in the “settings.py”:
TEMPLATE_DIRS=("/home/html/project/templates",)
MEDIA_ROOT="/home/html/project/appmedia/"
The problems will coming when you deployed to the production environment.
this way is recommended.
SITE_ROOT=os.path.realpath(os.path.dirname(__file__)) MEDIA_ROOR=os.path.join(SITE_ROOT,'appmedia') TEMPLATE_DIRS=(os.path.join(SITE_ROOT,'templates'),)
you also can use abspath, you can find the different with realpath in http://rob.cogit8.org/blog/2009/May/05/django-and-relativity-updated/
3.Don’t hearcoded static file in tempaltes.
Don’t use the following ways when use CSS, Javascript or images in templates.
<link rel="stylesheet" type="text/css" hef="/appmedia/amazing.css" /> <script type="text/javascript" src="/appmedia/jquery.min.js"></script>
if you move to another server, you must manual change the address one by one.
so it’s good way to use {{ MEDIA_URL }}
<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}amazing.css" />
<script type="text/javascript" src="{{MEDIA_URL}}jquery.min.js"></script>
Use “RequestContext” to get the template context.
return render_to_response("app/template.html",{'var':'foo'},context_instance=RequestContext(request))
you also can get current user information from “RequestContext”.
4.Don’t write normal logic in “views.py”
Write normal logic in views, it’s bad for reuse code.
it’s commended put in models or create a helper model.
Yes. if you get the Author from models, you can put the code in to view for get Author list.
5.Don’t forget change DEBUG to False when release.
One Way:
import socket
if socket.gethostname() == 'productionserver.com'
DEBUG=False
else:
DEBUG=True
Another Way:
#File Name: settings_debuy.py #the setting information include test models #Use "python manage.py runserver settings=settings_debug.py" to run project. from settings import * DEBUG=True #you also setting more variables which used in testing.
6.Loading one time template tag.
Normally, we loading template tags in templates.
{% load template_tags %}
We must write the same code in pages everytime to load tempalte tag.
So the following code is very cool!
from django import template
template.add_to_builtins('app.templatetags.custom_tag_module')
It's only put the code in the modules which is loading when project start running. i.e. settings.py, urls.py, models.py etc..
the above code can loading custom tag modules or filters when start running project. They can be used in template.
7.The rational allocation and use URL.
Don't put all url settings in one "urls.py". It's better set the URL in app's urls.py. the static file path don't use hard-coded. url also don't use hard-coded. otherwise you must change so many when you change one url. it's better to use url function. In /project/askalumini/urls.py, define the name for each url, it can help us to effectively deal with the view url, templates and models, rather than hard-coded. To ensure that the name of the only, please follow the url named <appname> / <somelabel> idiom. For example, in views.py have the code.
HttpResponseRedirect("/askalumini/questions/54")
please change to:
from django.core.urlresolvers import reverse
HttpResponseRedirect(reverse('askquestiondisploy',kwargs={'questionno':q.id}))
Use models.permalink decorator in the model used to format url:
@models.permalink
def get_absolute_url(self):
return ('profileur12',(),{'userid':self.user.id})
Url tag instead of using hard-coded in the template:
{% url askquestiondisploy 345%}
<a href="{% url askquestiondisploy 345 %}">Ask Question</a>
8.Debugging.
Debugging usually with some third-party tools to get more runtime information.
You can use django-debug-toolbar to see debugging information.
Another tool is Werkzeug debugger, it can open the python shell on the wrong page, allows you to more easily track the error message
There pdb, a powerful debugging tool: http://ericholscher.com/blog/2008/aug/31/using-pdb-python-debugger-django-debugging-series-/
9.Learn Pinax.
Django biggest advantage is code reuse, DRY, pinax is such a platform that contains a lot of code can be used directly, for example openid, email verification, and so on. Please visit: http: //pinaxproject.com/
10.Learn some useful third-party applications.
1) Database Upgrade Tool
What’s Database upgrade tool? you use “syncdb”, after one year, you changed models, do you want contunue to run “syncdb”? or “ALTER TABLE”..?
South is a stronger solution, but you must to learning how to use: http://south.aeracode.org/
2) Templates System
Jinja is a complete system of third-party template, you can replace the default template system, which offers many superior characteristics.
3) Third-party applications
django command extensions provide a lot of useful command-line functions:
shell_plus load all django model
runserver_plus integrated debugging tools Werkzeug
Generation model diagram, you can show it to your boss