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: