Django从Django管理编辑模板

2024-09-29 21:35:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以在我的数据库中保存模板并从django管理编辑它们吗? 我的想法是为模板创建一个模型:

class DbTemplate(models.Model):
    css_class = models.CharField(max_length=200)
    name = models.CharField(max_length=200)
    content = models.TextField()

    class Meta:
        verbose_name = "Database Template"
        verbose_name_plural = "Database Templates"

    def __str__(self):
        return self.name

并在django admin中注册:

^{pr2}$

在我的网址.py为加载正确模板的r“^page/”设置默认视图,是否可以?在

谢谢!在


Tags: djangoname模型self模板数据库编辑verbose
1条回答
网友
1楼 · 发布于 2024-09-29 21:35:48

您可能需要在视图中手动生成^{}模板并返回结果^{}。下面是一个粗略的示例(您应该根据您的用例对其进行修改):

from django.template import Context, Template
from django.http import HttpResponse

def my_view(request):
    ...
    context = {'value': something, 'another_var': something_else}
    # Here, instead of returning `render`, render the template using the context manually
    template_content = DbTemplate.objects.get(name='my_template').content
    template = Template(template_content)
    return HttpResponse(template.render(Context(context)))

相关问题 更多 >

    热门问题