[Django]-Using template in Django tutorial

2πŸ‘

βœ…

You can do this in the python manage.py shell, since that would have settings.py in . directory.

Otherwise:

Consider settings.py just another python library and you need to ensure its added to sys.path and then you can do as follows :

import settings
from django.core.management import setup_environ

setup_environ(settings)  # Setting up the env settings

This sets up the settings to your environment. [ This is the right way ]

There is no secret recipe in settings.py and since django in itself is a library to python, so it could not be default set any paths to your sys.path unless imported and executed – as I suggested.

πŸ‘€Yugal Jindle

1πŸ‘

You should really use setup_environ as suggested in another answer.

A hint regarding this specific exception: DJANGO_SETTINGS_MODULE is a Python module name, so it may not end with β€œ.py”. Usually, this would be your_django_site.settings and the module/directory your_django_site has to be in sys.path.

πŸ‘€AndiDog

0πŸ‘

All Django code not in a project should either be run from the Django REPL, available via ./manage.py shell, or run as a standalone script.

0πŸ‘

In order to test anything in the shell for Django, you should use the Django shell:

python manage.py shell

This will start up a Python interpreter which will magically have your settings file (and the rest of your project) on the Python path.

(This assumes you have a project already. If not, use django-admin.py startproject my_project_name.)

Leave a comment