/ / Wyjątki przy użyciu samodzielnego django z python3 - python, django, python-3.x

Wyjątki przy użyciu autonomicznego django z python3 - python, django, python-3.x

Próbuję używać szablonów django w trybie autonomicznym. Dostaję te wyjątki (poniżej). Nowość w pytonie, zastanawiasz się, czy ktoś byłby chętny do pomocy.

Django służy do tworzenia szablonów w skrypcie, który nie jest tutaj pokazany. Jednak przy uruchamianiu pojawiają się dokładnie te same wyjątki.

>>> from django.template import Template, Context
>>> from django.conf import settings
>>> settings.configure()
>>> t = Template("My name is {{ my_name }}.")
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/django/template/utils.py", line 86, in __getitem__
return self._engines[alias]
KeyError: "django"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/django/template/base.py", line 182, in __init__
engine = Engine.get_default()
File "/usr/lib/python3.4/functools.py", line 472, in wrapper
result = user_function(*args, **kwds)
File "/usr/local/lib/python3.4/dist-packages/django/template/engine.py", line 88, in get_default
django_engines = [engine for engine in engines.all()
File "/usr/local/lib/python3.4/dist-packages/django/template/utils.py", line 110, in all
return [self[alias] for alias in self]
....
....
File "/usr/local/lib/python3.4/dist-packages/django/apps/registry.py", line 137, in get_app_configs
self.check_apps_ready()
File "/usr/local/lib/python3.4/dist-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren"t loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren"t loaded yet.

Moja wersja django:

python3 -c "import django; print(django.get_version())" ---> 1.9.7

Moja wersja Pythona:

Python 3.4.3

Odpowiedzi:

2 dla odpowiedzi № 1

Po telefonie settings.configure(), musisz zadzwonić django.setup().

import django
from django.conf import settings
settings.configure()
django.setup()
from django.template import Template, Context
t = Template("My name is {{ my_name }}.")
c=Context({"my_name": "Mindaugas"})
t.render(c)

Widzieć doktorzy po więcej informacji.