S
S
Sergey Nizhny Novgorod2016-01-08 17:06:55
Django
Sergey Nizhny Novgorod, 2016-01-08 17:06:55

What is the correct way to display stars in Django reviews?

Hello.
Task: A person leaves a review (text) and the number of stars (number), and this information appears on the site: text - text, and the numbers are replaced by stars (image) (number 3 = three stars, etc.).
As a result:
6b3a5c246fa34c4c9ef17a8be8196055.jpg
I did it like this:
1) I added a method to the model that displays the number of pictures (stars), depending on the number that the person gives us.

class Mention(models.Model):
    mentionn = models.ForeignKey(Step, on_delete=models.CASCADE)
    mention_text = models.TextField()
    mention_digit = models.IntegerField()

    def get_star(self):
        if self.mention_digit == 1:
            return '<img src="../static/bakot/imagination/starfull.ico">'*1
        elif self.mention_digit == 2:
            return '<img src="../static/bakot/imagination/starfull.ico">'*2
        elif self.mention_digit == 3:
            return '<img src="../static/bakot/imagination/starfull.ico">'*3
        elif self.mention_digit == 4:
            return '<img src="../static/bakot/imagination/starfull.ico">'*4
        elif self.mention_digit == 5:
            return '<img src="../static/bakot/imagination/starfull.ico">'*5
        else:
            return self.mention_digit

2) I just inserted this method into the template in safe mode. That is, just hardcore html code.
<div class="small-12 medium-12 large-6 columns">
    <ul class="menu pdding_h44">
        <p>{{ men.get_star|safe }}</p>
    </ul>
</div>

Problem: My implementation works, but I'm sure that I did it in a very stupid and stupid way.
Tried replacing hardcore links with links like: didn't work anymore.
return '<img src="{% static "bakot/imagination/starfull.ico" %}">'*4

Can anyone suggest the correct logic for how this should be done?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question