[Django] how to load Templates using relative paths

Source: Internet
Author: User

Modify the template_dirs value in settings. py.

Annotations provided by Django are described as follows:

# Put strings here, like "/home/html/django_templates" or "C:/www/Django/templates ".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.

That is to say, Django does not recommend using relative paths.

How can I load Templates using relative paths when deploying them to a site?

You can use the following method:

Import OS. Path

Template_dirs = (

OS. Path. Join (OS. Path. dirname (_ file _), 'templates'). Replace ('\\','/'),

)

In this case, the path that Django will load is under the directory where the settings. py file is located.Templates folder.


For example, my site deployment is as follows:


You only need to make a slight modification:

Import OS. Path
Template_dirs = (
OS. Path. Join (OS. Path. dirname (_ file _), '../templates'). Replace ('\\','/'),
)

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.