Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. However the admin still shows the raw HTML content and not formatted content. 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)
like image 719
user2109249 Avatar asked Dec 26 '22 04:12

user2109249


1 Answers

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:

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!

like image 82
Paulo Bu Avatar answered Jan 04 '23 14:01

Paulo Bu