Home > Software design >  how to separate sentences and assign each sentence to a list element on template
how to separate sentences and assign each sentence to a list element on template

Time:04-16

how do I separate the list of strings by "." and render them into separate "li" elements on the template. Currently, it just displays everything in one "li" element. I would really appreciate it if someone can help, thx!

models.py

class Product(models.Model):
    benifits = ArrayField(models.CharField(max_length=800), blank=True)

    @property
    def benifits_slicing(self):
        benifit = self.benifits
        for point in benifit:
            test = point.split(".")
        return point

views.py

def described_view(request, slug):
    products = Product.objects.get(slug=slug)
    context = {"products": products}

HTML

<div id="BENIFITS">
    <li >{{ products.benifits_slicing }}</li>
</div>

what it looks like rn:

enter image description here

CodePudding user response:

You can try to do something like this...

class Product(models.Model):
    benifits = ArrayField(models.CharField(max_length=800), blank=True)

    @property
    def benifits_slicing(self):
        benifit = self.benifits
        data = []
        for point in benifit:
            data.extend(point.split("."))
        return data

HTML

<div id="BENIFITS">
    {% with data_benefit=products.benifits_slicing %}
    {% for data in data_benefit %}
    <li >{{ data }}</li>
    {% endfor %}
    {% endwith %}
</div>
  • Related