0๐
We can fix this in two ways:
- we store the color in a separate field; or
- 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>
- [Answered ]-Add Template to Django Oscar e-commerce framework
- [Answered ]-Django how to dynamically select between __gt and __range
- [Answered ]-Django Admin Login Background
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.
- [Answered ]-How to save many to many relationship?
- [Answered ]-Test Login with credentials as superuser Django 1.9, Python 3.5
- [Answered ]-How to access json return values in angular.js action success callback