Django 2.2';安全文本';对象没有属性';获取';

Django 2.2';安全文本';对象没有属性';获取';,django,django-views,django-templates,Django,Django Views,Django Templates,我正在一个项目中使用Django 2.2。我正在推出一个额外的轻量级应用程序,允许用户修改模板中使用的原始HTML 这是我编写的函数的简化示例: def show_page(request): from django.template import Context, Template template = Template("<h3>My name is {{ my_name }}.</h3>") context = Cont

我正在一个项目中使用Django 2.2。我正在推出一个额外的轻量级应用程序,允许用户修改模板中使用的原始HTML

这是我编写的函数的简化示例:

def show_page(request):
    from django.template import Context, Template
    template = Template("<h3>My name is {{ my_name }}.</h3>")

    context = Context({"my_name": "Adrian"})
    return template.render(context)  # <- Django barfs at this line
def显示页面(请求):
从django.template导入上下文,模板
template=template(“我的名字是{{My_name}}”)
context=context({“我的名字”:“阿德里安”})

return template.render(context)#假设
show_页面
是Django视图,则应返回
响应
对象,而不是模板字符串

通过添加以下内容更改您的视图:

从django.http导入HttpResponse
返回HttpResponse(template.render(context))

假设
show\u page
是Django视图,您应该返回
响应
对象,而不是模板字符串

通过添加以下内容更改您的视图:

从django.http导入HttpResponse
返回HttpResponse(template.render(context))
来自Django的

您编写的每个视图都负责实例化、填充和返回HttpResponse

当前您返回的是一个
django.utils.safestring.safestring
,它只是一个子类字符串

一个str子类,专门标记为HTML的“安全” 输出 目的

来自Django

您编写的每个视图都负责实例化、填充和返回HttpResponse

当前您返回的是一个
django.utils.safestring.safestring
,它只是一个子类字符串

一个str子类,专门标记为HTML的“安全” 输出 目的

return HttpResponse(template.render(context))