How do I use Django templates without the rest of Django?

后端 未结 13 1016
天命终不由人
天命终不由人 2020-11-29 16:17

I want to use the Django template engine in my (Python) code, but I\'m not building a Django-based web site. How do I use it without having a settings.py file (and others)

相关标签:
13条回答
  • 2020-11-29 16:54

    I would also recommend jinja2. There is a nice article on django vs. jinja2 that gives some in-detail information on why you should prefere the later.

    0 讨论(0)
  • 2020-11-29 16:54

    An addition to what other wrote, if you want to use Django Template on Django > 1.7, you must give your settings.configure(...) call the TEMPLATES variable and call django.setup() like this :

    from django.conf import settings
    
    settings.configure(TEMPLATES=[
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': ['.'], # if you want the templates from a file
            'APP_DIRS': False, # we have no apps
        },
    ])
    
    import django
    django.setup()
    

    Then you can load your template like normally, from a string :

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

    And if you wrote the DIRS variable in the .configure, from the disk :

    from django.template.loader import get_template
    t = get_template('a.html')
    t.render({'name': 5})
    

    Django Error: No DjangoTemplates backend is configured

    http://django.readthedocs.io/en/latest/releases/1.7.html#standalone-scripts

    0 讨论(0)
  • 2020-11-29 16:54

    I would say Jinja as well. It is definitely more powerful than Django Templating Engine and it is stand alone.

    If this was an external plug to an existing Django application, you could create a custom command and use the templating engine within your projects environment. Like this;

    manage.py generatereports --format=html
    

    But I don't think it is worth just using the Django Templating Engine instead of Jinja.

    0 讨论(0)
  • 2020-11-29 16:55

    Any particular reason you want to use Django's templates? Both Jinja and Genshi are, in my opinion, superior.


    If you really want to, then see the Django documentation on settings.py. Especially the section "Using settings without setting DJANGO_SETTINGS_MODULE". Use something like this:

    from django.conf import settings
    settings.configure (FOO='bar') # Your settings go here
    
    0 讨论(0)
  • 2020-11-29 17:00

    I echo the above statements. Jinja 2 is a pretty good superset of Django templates for general use. I think they're working on making the Django templates a little less coupled to the settings.py, but Jinja should do well for you.

    0 讨论(0)
  • 2020-11-29 17:05

    While running the manage.py shell:

    >>> from django import template   
    >>> t = template.Template('My name is {{ me }}.')   
    >>> c = template.Context({'me': 'ShuJi'})   
    >>> t.render(c)
    
    0 讨论(0)
提交回复
热议问题