[Answered ]-How can I color a title in django's sqlite database?

0๐Ÿ‘

โœ…

We can fix this in two ways:

  1. we store the color in a separate field; or
  2. we write the title in HTML syntax and then render this in the template.

Option 1: storing the color in a field

You can add a ColorField to your model from the django-colorfield package. You install this with:

pip3 install django-colorfield

we can then add the colorfield app to the INSTALLED_APPS:

# settings.py

# โ€ฆ

INSTALLED_APPS = [
    # โ€ฆ,
    'colorfield',
    # โ€ฆ
]

# โ€ฆ

Next we can add a ColorField to your model:

from colorfield.fields import ColorField
from django.db import models

class Aziende(models.Model):
    immagine = models.ImageField()
    nome = models.CharField(max_length=250)
    prezzo = models.FloatField()
    descrizione = models.CharField(max_length=250)
    nome_color = ColorField(default='#FF0000')

    def __str__(self):
        return self.nome

Then in the template, we can render the title with the given color. For example:

<p color="{{ item.nome_color }}"> {{ item.nome }}</p>

Option 2: storing HTML as title

We can also decide to store HTML data in the nome field. In that case we thus do not add an extra field to the model. We can use the mark_safe function to prevent Django from escaping the HTML code, so:

from colorfield.fields import ColorField
from django.db import models
from django.utils.safestring import mark_safe

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 mark_safe(self.nome)

We can then render the nome of the Aziende object with:

{{ aziende_item }}

or we can use the |safe template filter [Django-doc] to prevent escaping the name:

{{ aziende_item.nome|safe }}

1๐Ÿ‘

Store the color as a hex code in a 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

Then display it in your template:

<h1 class="title" style="color: {{azienda.colore}}" >{{azienda.nome}}</h1>
๐Ÿ‘คlucutzu33

0๐Ÿ‘

Create another app call it color, and another model call it Color, then in your current model u just add a foreignkey like this:

from django.db import models
from color.models import Color

#Create your models here.

class Aziende(models.Model):
   color = models.ForeignKey(Color, on_delete=models.DO_NOTHING) #Color is your new model

in your new model.py:

from django.db import models
class Color(models.Model):
    color=models.CharField()

    def __str__(self):
        return self.color

This way you can add any color you wish later on and then be able to choose it anytime you add it.

๐Ÿ‘คHello World

Leave a comment