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[1] - 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?
>
>
>
> --
>
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 [email protected].
>
To post to this group, send email to [email protected].
>
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[2].
>
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
Links:
1.
https://habrastorage.org/files/6b3/a5c/246/6b3a5c246fa34c4c9ef17a8be8196055.jpg
2.
https://groups.google.com/d/msgid/django-users/f1a31410-3959-42a2-9174-27756d7cbda1%40googlegroups.com?utm_medium=email&utm_source=footer
--
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 [email protected].
To post to this group, send email to [email protected].
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/1452307839.2953151.487082762.112A40E9%40webmail.messagingengine.com.
For more options, visit https://groups.google.com/d/optout.