Home > Enterprise >  How to display different Charfields into one sentence?
How to display different Charfields into one sentence?

Time:11-28

I'm doing a Django project that it is basically an ecommerce but for cars. My model is the following:

 class Userpost(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    title = models.CharField(max_length = 500)
    Year = models.CharField(max_length = 4)
    Mileage = models.CharField(max_length = 8)
    Make = models.CharField(max_length = 50)
    Model = models.CharField(max_length = 50)
    Price = models.DecimalField(max_digits=15, decimal_places=2)
    email = models.EmailField()
    date_published = models.DateField(default = timezone.now)

and my template is the following:

{% extends 'store/main.html' %}
{% load static %}

{% block content %}

<div class="row">
    {% for car in cars %}
    <div class="col-lg-4">
        <img class="thumbnail" src="{% static 'images/placeholder.png' %}">
        <div class="box-element product">
            <h6><strong>{{car.title}}</strong></h6>
            <hr>

            <button  class="btn btn-outline-secondary add-btn">Add to Cart</button>
            <a class="btn btn-outline-success" href="#">View</a>
            <h4 style="display: inline-block; float: right"> 
            <strong>${{car.Price|floatformat:2}}</strong></h4>

        </div>
    </div>
    {% endfor %}
</div>
{% endblock content %}

I was wondering if there is a way that i could remove the title from my model and instead of displaying {{car.title}}, I would display something like {{car.Year car.Make car.Model}}

CodePudding user response:

You can't use the concatenation like above in the templates. But you can use this instead,

<h6><strong>{{car.Year}} {{car.Make}} {{car.Model}}</strong></h6>
  • Related