Some practical suggestions for using Python's Django framework

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 planning, according to the official blog, Django1.5 will be experimental support Python3.

Django, as an excellent open source framework for Python, may not be as much of a compliment as other popular frameworks such as rails, but it is as refined as any other framework, paying great attention to the dry (Don ' t Repeat Yoursef) principle, the reusability of components, The automation process makes coding more concise.

If you have the flexibility to use certain methods and techniques in a Django project, it will greatly speed up software development and avoid many headaches. The authors list several points below, which are easy to use and can help any level of programmer to be more adept at using Django.

0. Using relative paths in configuration

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

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

1. Use {%url%} tags

Use the backward-compatible {%url%} tag as much as possible to replace the hard-coded href with the same effect as the URL that uses the absolute path (and, of course, it is best not to do so). Your Django project will not be affected by the migration of those links. (Translator Note: For example, we have a views.about function pointing to the About page R ' ^about/$ ', you can {% URL views.about as about_url%} and then use {{About_url}} This variable instead of the absolute URL address) Although it is not the most advanced technique, it is really worth applying to your Django project.
2. Try to apply the Django Admin to the PHP project

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

3. Use a standalone Media server

It is not a problem to put static files in the development environment on the same server as the Django project, but do not use them in a production environment. Efficiency issues. Jacobian.org gives a reasonable explanation. With a separate server to handle static files, performance will be effectively improved, and if you do not want to buy a server, then using Amazon S3 is relatively cheaper.

4. Using the Debugger tool bar

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

5. Using Django Unit Test

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

6. Use speed Check Card

There are two pages of fast-track cards, and in the Django documentation you may be tossing and turning for half a day. It contains several topics such as

Template:

Template labels and optional options

Template Filters and Options

Quick lookup of date formatting syntax

Model:

Domain and and options

Optional options for common domains

Meta-type selectable options

Model Management available options

Form:

Fields and selectable options

Common domain Options

Standard error message key value

7. Using Django-chunks

In addition to creating blocks with Django's rich text editor, Django-chunks is also used in templates, which is an essential tool for reusing code blocks.

8. Using Memcache

If performance has become a tricky issue in your Django project, then you will need to use some caching strategies. However, Django offers a number of options for caching. At the moment the best is undoubtedly memcache, with Django installed memcache very simple if you use the Cmemcache module. As soon as the module is installed, you simply modify a single line of configuration items and your Django page becomes lighter.

9, the use of Django, the Heartbeat than action

After you read this article, if you still don't fully understand the power of Django, one reason to use Django in your next project is that it saves time on different software designs. Jeff Croft explains why creating a project with Django is more efficient than designing it yourself. Django allows you to expand your Web site without worrying about design or code and database compatibility, and it 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.