Django Admin shows escaped HTML even when allow_tags = True

I have the following code for model and admin. The question column contains HTML content such as a URL and image tags. However, the administrator still displays the original HTML content and non-formatted content. The model and admin code are below:

Model

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

Administrator

 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) 
+4
source share
1 answer

Is that your code? You have formatqn.allow_tags=True indented inside the def formatqn method after returning, so it will never execute, try writing a model with this line that doesn't matter:

 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 this helps!

+10
source

Source: https://habr.com/ru/post/1480150/


All Articles