Django渲染模板标签从数据库到html

时间:2018-10-30 16:19:46

标签: django database templates variables

如何将模板标签从models.TextField呈现为html。 在我的django管理员中:

Django admin to edit content picture

我打算用一些所见即所得的编辑器扩展内容。

在wiews.py中,我定义了如何将其呈现为html格式:

from django.shortcuts import render
from .models import Email

def EmailData(request, slug):

   email = Email.objects.get(slug=slug)

   return render(request, email.template.path,{
       "email": email
   })

HTML:

{% extends "baseEmail.html" %}
{% block emailContent %}

        <h1>{{ email.email }}</h1>
        <h2>{{ email.subject }}</h2>

        <h2>{{ email.content|safe }}</h2>

        <h2>{{ email.created }}</h2>
        <h2>{{ email.sendToAdmin }}</h2>
        <h2>{{ email.template.path }}</h2>
{% endblock %}

h1标签正确显示,但变量未正确显示。如何从文本字段呈现变量集?

这是结果:

Web site result

电子邮件模式:

import os
from django.urls import reverse
from django.db import models
from django.conf import settings
from django.utils.text import slugify

class Category(models.Model):
    id                 = models.AutoField(primary_key=True)
    Name               = models.CharField(max_length=70)

    def __str__(self):
        return self.Name


class Template(models.Model):
    id                 = models.AutoField(primary_key=True)
    Name               = models.CharField(max_length=70, blank=True)
    Folder          =models.ForeignKey(Category,on_delete=models.SET_NULL, blank=True, null=True)
    templateName       = models.CharField(max_length=70)
    path               = models.CharField(max_length=200, blank=True)

    def __str__(self):
        return self.Name

    def _get_path_to_template(self):

## SAVE DIRNAMES TO DATABASE

    dir = settings.EMAILS_TEMPLATE
    allDirs =os.listdir(dir)
    for dirname in allDirs:
        if Category.objects.filter(Name=dirname).exists():
            pass
        else:
            Category.objects.create(Name = dirname)

## GET TEMPLATE PATH

    name     = self.templateName
    category = str(self.Folder)
    fullPath = category + "/" + name + '.html'
    return fullPath


def save(self, *args, **kwargs):
    self.path = self._get_path_to_template()
    super().save()




class Email(models.Model):
    id              = models.AutoField(primary_key=True)
    email           = models.CharField(max_length=70)
    subject         = models.CharField(max_length=70)
    content         = models.TextField(blank=True)
   template        = models.ForeignKey(Template, on_delete=models.CASCADE)

   sendToAdmin     = models.BooleanField(default=False)
   failSilently    = models.BooleanField(default=True)

   slug            = models.SlugField(max_length=100, 
   db_index=True,blank=True)
updated         = models.DateTimeField(auto_now=True, auto_now_add=False)
created         = models.DateTimeField(auto_now=False, auto_now_add=True)

def __str__(self):
    return self.email

def get_absolute_url(self):
    return reverse("emails", kwargs={'slug' : self.slug})

def _get_unique_slug(self):
    slug = slugify(self.email)
    unique_slug = slug
    num = 1
    while Email.objects.filter(slug=unique_slug).exists():
        unique_slug = '{}-{}'.format(slug, num)
        num += 1
    return unique_slug

def save(self, *args, **kwargs):

    self.slug = self._get_unique_slug()

    super().save()

0 个答案:

没有答案