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_digit2) 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" %}">'*3Could 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+unsubscribe@googlegroups.com.To post to this group, send email to django-users@googlegroups.com.Visit this group at https://groups.google.com/group/django-users.To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f1a31410-3959-42a2-9174-27756d7cbda1%40googlegroups.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
No comments:
Post a Comment