This section briefly introduces how to load a template using the Python Django framework.

Source: Internet
Author: User

This section briefly introduces how to load a template using the Python Django framework.

Generally, you store templates in the file system as files, but you can also use custom template loaders to load templates from other sources.

Django has two ways to load templates

  1. Django. template. loader. get_template (template_name): get_template returns a compiled Template (a template object) based on the given Template Name ). If the template does not exist, the TemplateDoesNotExist exception is triggered.
  2. Django. template. loader. select_template (template_name_list): select_template is similar to get_template, but it uses the list of template names as parameters. It returns the first template in the list. If none of the templates exist, the TemplateDoesNotExist exception is triggered.

By default, these functions use the settings of TEMPLATE_DIRS to load the template. However, internal functions can specify a template loader to complete these heavy tasks.

Some loaders are disabled by default, but you can activate them by editing the TEMPLATE_LOADERS settings. TEMPLATE_LOADERS should be a string tuples. Each string represents a template loader. These template loaders are released along with Django.

Django. template. loaders. filesystem. load_template_source: This loader loads templates from the file system according to the settings of TEMPLATE_DIRS. It is available by default.

Django. template. loaders. app_directories.load_template_source: This loader loads templates from Django applications on the file system. For each application in INSTALLED_APPS, the loader looks for the templates subdirectory. If this directory exists, Django will look for a template there.

This means that you can save the template with your application, making it easier for the Django application to be released with the default template. For example, if INSTALLED_APPS contains ('myproject. polls', 'myproject. Music'), get_template('foo.html ') searches for the template in this order:

    /path/to/myproject/polls/templates/foo.html    /path/to/myproject/music/templates/foo.html

Note that the loader performs an optimization when it is imported for the first time: it caches a list that contains packages with the templates subdirectory in INSTALLED_APPS.

This loader is enabled by default.

Django. template. loaders. eggs. load_template_source: This loader is similar to app_directories, But it loads templates from Python eggs instead of the file system. This loader is disabled by default. If you use eggs to publish your application, you need to enable it. Python eggs can compress Python code into a file.

Django uses the template loader in the order set in TEMPLATE_LOADERS. It uses each loader one by one until a matching template is found.

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.