How to show review's stars

34 views
Skip to first unread message

Sergey Bakotin

unread,
Jan 8, 2016, 9:37:42 AM1/8/16
to Django users

Hello and thank your for your help:

Task: The customer leaves text and digit (the number of stars) i must display it on the page. The review - link.

How i done it:

1) Add method to the model:

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) Install the method to my template:

<div class="small-12 medium-12 large-6 columns">
   
<ul class="menu pdding_h44">
     
<p>{{ men.get_star|safe }}</p>
   
</ul>
</div>

The problem: It works, but  i know it is very stupied realization. I was trying to do it with with static files, like (below), but it didn't work.

return '<img src="{% static "bakot/imagination/starfull.ico" %}">'*3

Could someone suggest me the right way of realization this task?


Hugo Osvaldo Barrera

unread,
Jan 8, 2016, 9:51:30 PM1/8/16
to django...@googlegroups.com
 
On Fri, Jan 8, 2016, at 11:34, Sergey Bakotin wrote:


Hello and thank your for your help:
 
Task: The customer leaves text and digit (the number of stars) i must display it on the page. The review - link.
 
How i done it:
 
1) Add method to the model:
 




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

defget_star(self):
ifself.mention_digit ==1:
return'<img src="../static/bakot/imagination/starfull.ico">'*1
elifself.mention_digit ==2:
return'<img src="../static/bakot/imagination/starfull.ico">'*2
elifself.mention_digit ==3:
return'<img src="../static/bakot/imagination/starfull.ico">'*3
elifself.mention_digit ==4:
return'<img src="../static/bakot/imagination/starfull.ico">'*4
elifself.mention_digit ==5:
return'<img src="../static/bakot/imagination/starfull.ico">'*5
else:
returnself.mention_digit

 
2) Install the method to my template:
 
<divclass="small-12 medium-12 large-6 columns">
<ulclass="menu pdding_h44">

<p>{{ men.get_star|safe }}</p>
</ul>
</div>
 
The problem: It works, but  i know it is very stupied realization. I was trying to do it with with static files, like (below), but it didn't work.
 

return'<img src="{% static "bakot/imagination/starfull.ico" %}">'*3

 
Could someone suggest me the right way of realization this task?
 
 


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
 
Option (1) would break if you change your STATIC_URL. It's unmaintainable.
Option (2) doesnt work.
 
I'd go with something like:
 
    class Mention(...
        def get_stars(self):
            return "bakot/imagination/starfull.png"
 
And in your template use that:
 
    <img src="{% static mention.get_stars %}" alt="blah">...
 
This keeps static files stuff decoupled from the model (though you still have a filename there, ugh).
 
My final option is doing it PROPERLY, and would consist of creating a custom template tag, and use the staticfile_storage to get the full URL (you'll have to rtfm for both things). This will be more work, but you'll also learn more in the process.
 
--
Hugo Osvaldo Barrera
 
Reply all
Reply to author
Forward
0 new messages