首页 > 解决方案 > 如何在 django 的 sqlite 数据库中为标题着色?

问题描述

当我添加一张新卡时,我希望每次都可以选择标题颜色。这是我的代码:

from django.db import models

#Create your models here.
class Aziende(models.Model):
    immagine = models.ImageField()
    nome = models.CharField(max_length=250)
    prezzo = models.FloatField()
    descrizione = models.CharField(max_length=250)

    def __str__(self):
        return self.nome

我能怎么做?提前致谢!

标签: djangosqlitedjango-models

解决方案


将颜色作为十六进制代码存储在 CharField 中:

class Aziende(models.Model):
    immagine = models.ImageField()
    nome = models.CharField(max_length=250)
    prezzo = models.FloatField()
    descrizione = models.CharField(max_length=250)
    colore = models.CharField(max_length=10, default='#FFFFFF')

    def __str__(self):
        return self.nome

然后将其显示在您的模板中:

<h1 class="title" style="color: {{azienda.colore}}" >{{azienda.nome}}</h1>

推荐阅读