Some practical suggestions for using the Python Django framework

Source: Internet
Author: User
This article mainly introduces some practical suggestions for using the Python Django framework, including some excellent modules. For more information, see the following: With the release of the second candidate version of Django1.4, although Python3 is not yet supported, the Django team is already working on the plan. according to the official blog, Django1.5 will provide experimental support for python3.

Django, as an outstanding open-source Python framework, may not be as praised as other popular frameworks such as Rails, but it is as refined as other frameworks and focuses heavily on DRY (Don't Repeat Yoursef) principles, component reusability, more concise coding through automated processes.

If you can use some methods and techniques flexibly in the Django project, it will greatly speed up software development and avoid a lot of headaches. The author lists several points below. these methods are from a simple perspective and can help programmers at any level use Django more skillfully.

0. use the relative path in the configuration

For some reason, projects may often be migrated back and forth. If this possibility is not planned in advance, this is definitely a tricky problem. Rob Hudson has an excellent technique to ensure that your Django project can be easily migrated back and forth during deployment. Just write a few lines of code in your configuration file (settings. py.

import osBASE_DIR = os.path.dirname(os.path.abspath(__file__)) TEMPLATE_DIRS = (  BASE_DIR + '/templates',)

1. use the {% url %} tag

Try to replace the hard-coded href with the backward compatible {% url %} tag, which is the same as the url with an absolute path (of course it is best not to do this. Those links will not be affected when your Django project is migrated. For example, we have a view. the about function points to the about page R' ^ about/$ 'to {% url views. about as about_url %} and use the variable {about_url} to replace the absolute URL address.) although it is not the most advanced technique, it is indeed worth applying to Django projects.
2. try to apply Django admin to the PHP project.

One of Django's greatest features is that it has become a user verification system for Django's core functions. It is easy to install and mainly used for user authentication and other necessary configurations. This cool user system is even recommended to be applied to your PHP project, here is a good solution for Jeff Croft about why Django can be used as a system management module in any application language.

3. use an independent media server

It is not a big problem to place static files on the same server as the Django project in the development environment, but it should not be used in the production environment. why? Efficiency issues. Jacbian.org provides a reasonable explanation. Processing static files through an independent server can effectively improve the performance. if you do not want to buy a server, Amazon S3 is cheaper.

4. use the Debugger toolbar

Debugging tools are indispensable for any language. they can speed up development and point out potential defects. Rob Hudson developed a django debugging tool that is very useful to developers.

5. use Django unit test

Use unit tests to ensure that your code changes are the same as expected without disrupting any old code for backward compatibility. A powerful feature of Django is that it can easily write unit tests. Django can also directly use python text testing and unit testing. The Django document provides a detailed tutorial and sample code about how to perform unit tests to make the code run correctly and remove annoying bugs.

6. Use a speed query card

There are two pages of high-speed scanning cards. in the Django document, you may need to find things for half a day at a glance here. It contains the following topics:

Template:

Template tags and options

Template filter and optional

Quick query of date formatting syntax

Model:

Fields and options

Options for common domains

Metadata option

Model Management option

Form:

Domain and optional

Common domain options

Standard error message key value

7. use Django-chunks

Apart from using Django's rich text editor to create blocks, Django-chunks is also used in templates, which is an essential tool for reusing code blocks.

8. use Memcache

If performance has become a tricky issue in your Django project, you will need to use some Caching policies. However, Django provides many choices for caching. At present, the best is undoubtedly Memcache. it is very simple to install memcache with Django. if you use the cmemcache module. After the module is installed, you only need to modify one configuration line, and your Django page becomes brisk.

9. Django is not as exciting as action

After reading this article, if you still do not fully understand the power of Django, the reason for using Django in your next project is: it can save time for different software designs. Jeff Croft explains why using Django to create a project is more efficient than you have designed. Django allows you to expand your Web site without worrying about the design, code, and database compatibility. it will work very well.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.