I
I
Igor2019-05-06 18:22:20
Django
Igor, 2019-05-06 18:22:20

How to insert html tags (CSS) in response from Django?

Good afternoon! I wanted to have a double column in the admin panel on the model instance selection page - in one column two values ​​\u200b\u200bdisplayed at once, like this:

Example
╔═══════════╦═════════════╦══════════════════════════════════════╗
║  Предмет  ║  Категория  ║ Количество взятых / Общее количество ║
╠═══════════╬═════════════╬══════════════════════════════════════╣
║ Предмет 1 ║ Категория 1 ║               123 / 155              ║
╚═══════════╩═════════════╩══════════════════════════════════════╝


The trick is that the slash (/) should be logically highlighted ( be of a different color ) and I didn’t find anything other than to include a tag with inline style in the returned html string , besides, I remember that somewhere in the official Django tutorials I have seen a similar design.
admin.py
class ItemAdmin(admin.ModelAdmin):
    def get_numbers(self):
        return str(self.number_taken)+ '<span style="color: #6161ff">/</span>' + str(self.number_full)
    get_numbers.short_description = 'Количество взятых <span style="color: #6161ff">/</span> общее количество'

    list_display = ['name', 'category', get_numbers]

As a result, I did not get what I wanted at all, namely:
5cd04e5fbe1ea106976950.pngQUESTION: how to do what I had in mind?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2019-05-06
@Igorello74

Use mark_safe which will disable escaping for the given string in the template.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question