Using a Template in a Django Tutorial

I am in Chapter 4: Django Tutorial Templates, and I am trying to run the following in lower Emacs mode using Python:

>>> from django import template
>>> t = template.Template('My name is {{ name }}.')
>>> c = template.Context({'name': 'Adrian'})
>>> print t.render(c)
My name is Adrian.
>>> c = template.Context({'name': 'Fred'})
>>> print t.render(c)
My name is Fred.

But I get an error here .

A quick search suggests that I should set up my DJANGO_SETTINGS_MODULE as follows:

>>> import os
>>> os.environ["DJANGO_SETTINGS_MODULE"] = "settings.py"

However, I ran into this error:

ImportError: Could not import settings 'settings.py' (Is it on sys.path?): No module named settings.py

I am using Fedora 16. How to solve this problem?

+3
source share
4 answers

You can do this in python manage.py shell, as it would have settings.pyin a directory ..

Otherwise:

Consider settings.pyonly one python library, and you need to make sure that it is added to sys.path, and then you can do the following:

import settings
from django.core.management import setup_environ

setup_environ(settings)  # Setting up the env settings

. [ ]

settings.py , django python, sys.path, - .

+2

setup_environ, .

: DJANGO_SETTINGS_MODULE - Python, ".py". your_django_site.settings, / your_django_site sys.path.

+1

Django, , Django REPL, ./manage.py shell, script.

0

- Django, Django:

python manage.py shell

Python, ( ) Python.

(It is assumed that you already have a project. If not, use it django-admin.py startproject my_project_name.)

0
source

All Articles