Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Inspired by the question series 'Hidden features of ...', I am curious to hear about your favourite Django tips or lesser-known but useful features you know of.

  • Please, include only one tip per answer.

  • Add Django version requirements if there are any.

1 Answer

0 votes
by (106k points)

The favourite, lesser-known and useful features in Django in my perspective will be as follows:- 

You can use os.path.dirname() in settings.py to avoid hard-coded directory names.

Never hardcode the path in your settings.py if you want to run your project in different locations. Use the following code in settings.py if your templates and static files are located within the Django project directory then use the below-mentioned code to access them:

import os
PROJECT_DIR = os.path.dirname(__file__)

STATIC_DOC_ROOT = os.path.join(PROJECT_DIR, "static") TEMPLATE_DIRS = ( 

os.path.join(PROJECT_DIR, "templates"), 

)

Related questions

0 votes
1 answer
0 votes
1 answer
asked Feb 25, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Feb 12, 2021 in Python by laddulakshana (16.4k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...