urls.py
url(r'^/mailing/(?P<pk>\d+)/preview/$', PreView.as_view(), name="preview"),
models.py
class Message(models.Model):
# ... other fields ...
body = models.TextField(_("Body"), help_text=_("You can use Django <a target='_blank' href='https://docs.djangoproject.com/en/dev/ref/templates/builtins/'>template tags</a>"))
views.py
class PreView(TemplateView):
template_name = "mailing/preview.html"
def get_context_data(self, pk, **kwargs):
try:
return {"message": Message.objects.get(id=pk)}
except Message.DoestNotExist:
raise Http404
template/mailing/preview.html
<div id="body">{{ message.body|safe }}</div>
however django templatetags are not interpreted, only rendered as a string. I would like to use a
{% now "Y-m-d" %}
tag in message body. In future there will be need to use any other tag.
I have managed two working approaches, both of them are not satisfying me.
- Use regexps and substitutions,
- Put whole template source in db TextField (insted of file), and renders a page(template) from it.
I am also thinking about creating templatetag which returns a rendered template out of Message.body. However I am not quite sure whether it will be good or wrong.
Do you have any suggestions?