# Using a string as template in CBV

**URL:** <https://forum.djangoproject.com/t/using-a-string-as-template-in-cbv/5797>\
**Category:** Using Django\
**Created:** [December 22, 2020, 1:14pm UTC](https://forum.djangoproject.com/t/using-a-string-as-template-in-cbv/5797 "2020-12-22T13:14:18Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [December 22, 2020, 7:32pm UTC](https://forum.djangoproject.com/t/using-a-string-as-template-in-cbv/5797/4 "2020-12-22T19:32:03Z")

</div>

Hey @valentinogagliardi — as I said on Twitter, you’re nearly there, but you’re probably importing the wrong class.

Compare [django.template.backends.django.Template](https://github.com/django/django/blob/8b2a30f6f16cb1f3538847954030d69da005bc7f/django/template/backends/django.py#L48-L58), which takes the `request` `TemplateResponse` is trying to pass to it with [django.template.base.Template](https://github.com/django/django/blob/8b2a30f6f16cb1f3538847954030d69da005bc7f/django/template/base.py#L140-L164), which doesn’t.

You’re not really ever meant to use `base.Template`. (Why’s it there? History…) As per the [Loading a template](https://docs.djangoproject.com/en/3.1/ref/templates/api/#loading-a-template) docs

> The recommended way to create a [`Template`](https://docs.djangoproject.com/en/3.1/ref/templates/api/#django.template.Template) is by calling the factory methods of the [`Engine`](https://docs.djangoproject.com/en/3.1/ref/templates/api/#django.template.Engine): [`get_template()`](https://docs.djangoproject.com/en/3.1/ref/templates/api/#django.template.Engine.get_template), [`select_template()`](https://docs.djangoproject.com/en/3.1/ref/templates/api/#django.template.Engine.select_template)and [`from_string()`](https://docs.djangoproject.com/en/3.1/ref/templates/api/#django.template.Engine.from_string).

You want `from_string()`:

```auto
>>> from django.template import engines
>>> django_engine = engines['django']
>>> template = django_engine.from_string("Hello {{ name }}!")
>>> template.render({"name":"Valentino"}, request=None)
'Hello Valentino!'

```

All those examples **not using the backend API** need to disappear, but likely never can.  
HTH

---

_[View the full topic](https://forum.djangoproject.com/t/using-a-string-as-template-in-cbv/5797)._
