10 Notes for beginners to learn Django

Source: Internet
Author: User
Ten Notes for beginners to learn about Django. These 10 notes can help you better learn Django, reduce errors, and avoid detours, it's worth watching ~~

1. do not include the project name in the reference code.
For example, if you create a project named "project" and contain an application named "app", the following code is not good:

from project.app.models import Author

Disadvantages: applications and projects become tightly coupled and cannot be easily reused. If you want to change the project name in the future, you may have to accept it.
The recommended practice is:

from app.models import Author

Note that you need to configure the Project path in PYTHONPATH.
2. do not hard encode MEDIA_ROOT and TEMPLATE_DIRS.
Do not use the following code in the settings. py project configuration file:

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

Problems may occur when you deploy the service to a production environment or migrate servers.
The following method is recommended:

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

(You can also use abspath, the difference with realpath please refer to the http://rob.cogit8.org/blog/2009/May/05/django-and-relativity-updated)
3. do not hard encode the path of the static file in the template.
The following method is not recommended when linking CSS, javascript, or images in the template:

 

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.