Some practical suggestions for using the Python Django framework _python

Source: Internet
Author: User

Foreword: With the release of Django1.4 's second candidate, although Python3 is not yet supported, the Django team is already in the process of planning, and according to the official blog, Django1.5 will be experimenting with supporting python3.

Django, as an outstanding Python open source framework, may not get as much praise as other popular frameworks like rails, but it is as refined as any other framework, paying much attention to the dry (Don ' t Repeat Yoursef) principles, the reusability of components, Automate the process to make your code more concise.

If you are able to use certain methods and techniques flexibly in your Django project, it will greatly speed up your software development and avoid a lot of headaches. The author lists several points below, which can help any level of programmer to use Django more skillfully.

0. Use relative path in configuration

For some reason, the project may often be migrated back and forth. This is definitely a tricky problem if you don't plan this possibility beforehand. Rob Hudson has an excellent technique to ensure that your Django project is easy to migrate back and forth during deployment. Just write a few lines of code in your configuration file (settings.py).

Import os
base_dir = Os.path.dirname (Os.path.abspath (__file__))
 
Template_dirs = (
  base_dir + '/templates ' ,
)

1. Use {%url%} label

Replace the hard-coded form of href with the backward-compatible {%url%} tag whenever possible, with the same effect as a URL that uses an absolute path (preferably not). Your Django project is migrated and those links will not be affected. (Translator Note: For example, we have a views.about function that points to about page R ' ^about/$ ', you can {% URL views.about as about_url%} and then use {{About_url}}} This variable is a substitute for an absolute URL address. Although it is not the most advanced technique, it does deserve to be applied to Django projects.
2. Try to apply Django Admin to the PHP project

One of the greatest features of Django is the user authentication system that has become the core feature of Django. It is easy to install, mainly for user authentication and some other necessary configuration. This cool user system is even recommended for use in your PHP project, with Jeff Croft a good solution to why Django can be used as a System management module in any application in any language.

3, the use of independent media server

It is not a big problem to put a static file on the same server as the Django project in the development environment, but don't use it in a production environment. Efficiency issues. Jacobian.org gives a reasonable explanation. With a stand-alone server to handle static files, performance will be effectively improved, if you do not want to buy a server, then use Amazon S3 relative to cheaper.

4, the Use of Debugger tool bar

Debugging tools are essential to any language. They can speed up development and point to potential flaws. Rob Hudson has developed a very useful Django debugging tool for developers.

5. Use Django Unit test

Use unit testing to ensure that your code changes as expected, without damaging any old code for backwards compatibility. A powerful feature of Django is that he can write unit tests very simply. Django can also use Python's text and unit tests directly. The Django document provides a detailed tutorial and sample code on how to do unit testing to make the code run correctly, and to remove pesky bugs

6. Use Quick Check Card

Here's a two-page, quick-check card that you might want to toss around for half a day in your Django document. It contains the following several topics

Template:

Template labels and optional options

Template filters and optional options

Date format Syntax Quick Lookup

Model:

Fields and and options

Optional options for common fields

Meta type can be selected

Model Management options

Form:

Fields and optional

Common fields can be selected

Standard error message key value

7. Use Django-chunks

In addition to using the Django 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 problem in your Django project, then you will need to use some caching strategies. However, Django offers a lot of options for caching. At present the best is undoubtedly memcache, with Django installation memcache very simple, if you use the Cmemcache module. As soon as the module is installed, you only modify one line of configuration items, and your Django page becomes brisk.

9, use Django, heartbeat as Action

If you still don't fully understand the power of Django after reading this article, the reason for using Django in your next project is to save time for different software designs. Jeff Croft explains why it's more efficient to create a project with Django than you design yourself. Django allows you to expand your Web site without worrying about design or code and database compatibility, and it will workplace great.

Related Article

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.