简体   繁体   English

即使allow_tags = True,Django Admin也会显示转义的HTML

[英]Django Admin shows escaped HTML even when allow_tags=True

I have the following code for the model and admin. 我有以下代码用于模型和管理员。 The question column contains HTML content such as URL and image tags. 问题列包含HTML内容,例如URL和图像标记。 However the admin still shows the raw HTML content and not formatted content. 但是,管理员仍然显示原始HTML内容而不是格式化内容。 The model and the admin code is below: 模型和管理代码如下:

Model 模型

class question(models.Model):
    question_id = models.AutoField(primary_key=True) # Unique ID
    question = models.TextField() # HTML Content for the question

Admin 管理员

class QuestionAdmin(admin.ModelAdmin):
    list_display = ('question_id','formatqn')
    list_per_page = 10 

    def formatqn(self, obj):
        return u'%s' % obj.question
        formatqn.allow_tags = True

admin.site.register(question, QuestionAdmin)

Is that your code exactly? 这是你的代码吗? You have formatqn.allow_tags=True indented inside the def formatqn method after the return so it won't execute never, try to write the model with that line unindented like this: 返回后你在def formatqn方法中缩进了formatqn.allow_tags=True ,所以它永远不会执行,尝试用这样的方式编写带有这条线的模型:

class QuestionAdmin(admin.ModelAdmin):
    list_display = ('question_id','formatqn')
    list_per_page = 10 

    def formatqn(self, obj):
        return u'%s' % obj.question

    # this line unindented
    formatqn.allow_tags = True

admin.site.register(question, QuestionAdmin)

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM