How to use Star rating of Font-awesome with Django

2020-05-14 09:57发布

Fontawesome has a great star rating css extension, which looks really awesome.

However clicking on any of the span elements wouldn't really do anything. I don't know how to hook this up with my database model. Lets say I have an integer field of 0-5 in Django. How could I set the value according to the user's selection within the template?

enter image description here

<span class="rating">
  <span class="star"></span>
  <span class="star"></span>
  <span class="star"></span>
  <span class="star"></span>
  <span class="star"></span>
</span>

11条回答
女痞
2楼-- · 2020-05-14 10:09

I seen this post, but didn't want to use a plugin with more than I needed. So, I threw this together for a small project I'm working on. You don't need bootstrap to use this.

HTML

<div class="star-rating"> 
  <span class="fa fa-star-o" data-rating="1"></span>
  <span class="fa fa-star-o" data-rating="2"></span>
  <span class="fa fa-star-o" data-rating="3"></span>
  <span class="fa fa-star-o" data-rating="4"></span>
  <span class="fa fa-star-o" data-rating="5"></span>
  <input type="hidden" name="whatever" class="rating-value" value="3">
</div>

CSS

.star-rating {
  line-height:32px;
  font-size:1.25em;
  cursor: pointer;
}

CoffeeScript

$star_rating = $('.star-rating .fa')

SetRatingStar = ->
  $star_rating.each ->
    if parseInt($star_rating.siblings('input.rating-value').val()) >= parseInt($(this).data('rating'))
      $(this).removeClass('fa-star-o').addClass('fa-star')
    else
      $(this).removeClass('fa-star').addClass('fa-star-o')

$star_rating.on 'click', ->
    $star_rating.siblings('input.rating-value').val $(this).data('rating')
    SetRatingStar()

SetRatingStar()

Javascript:

var $star_rating = $('.star-rating .fa');

var SetRatingStar = function() {
  return $star_rating.each(function() {
    if (parseInt($star_rating.siblings('input.rating-value').val()) >= parseInt($(this).data('rating'))) {
      return $(this).removeClass('fa-star-o').addClass('fa-star');
    } else {
      return $(this).removeClass('fa-star').addClass('fa-star-o');
    }
  });
};

$star_rating.on('click', function() {
  $star_rating.siblings('input.rating-value').val($(this).data('rating'));
  return SetRatingStar();
});

SetRatingStar();
查看更多
霸刀☆藐视天下
3楼-- · 2020-05-14 10:09

Here's a better solution. The stars are based only on the numbers, so the search engine and the browser just reads 4.5 out of 5 and that's all. It uses my own custom font; no JS, no SVG, no Flash, no Canvas. It's also available as a progress bar.

DEMO/CODE

Here the code, without the microformat tags to make it simpler:

Rating: <span class="star-rating-icons">
<strong>3.5</strong> out of <i>5</i>
</span>

And here's the CSS that includes the font and the relevant styling:

.star-rating-icons {
    font-family: seostars;
  font-size: 1.2em;
    line-height: .6em;
    position: relative;
    width: 5em;
    text-indent: -5000px;
    display: inline-block;
    font-style: normal;
}
.star-rating-icons strong {
    font-weight: normal;
    display: block;
    position: absolute;
    left: 0px;
    color: #FC0;
    font-family: seostars;
    text-indent: 0;
}
.star-rating-icons strong:first-letter {
    font-weight: bold;
    position: absolute;
    left: 0px;
    font-style: normal;
}
.star-rating-icons i {
    color: #666;
    position: absolute;
    text-indent: 0;
    color: #666;
    left: 0;
}

Basically the first-letter of the rating is formatted with a character with that amount of whole stars, and the decimal with the partial stars. The outline of the stars is made out of the period, although it could be applied to any other character present. (By editing the font or just with a pseudo element)

查看更多
爷、活的狠高调
4楼-- · 2020-05-14 10:11

One more interesting method. Pure CSS, keeps selected value, uses radio-buttons (cool!) and FA fonts

See demo

Taken from this post

HTML

<div class="star-rating">
  <div class="star-rating__wrap">
    <input class="star-rating__input" id="star-rating-5" type="radio" name="rating" value="5">
    <label class="star-rating__ico fa fa-star-o fa-lg" for="star-rating-5" title="5 out of 5 stars"></label>
    <input class="star-rating__input" id="star-rating-4" type="radio" name="rating" value="4">
    <label class="star-rating__ico fa fa-star-o fa-lg" for="star-rating-4" title="4 out of 5 stars"></label>
    <input class="star-rating__input" id="star-rating-3" type="radio" name="rating" value="3">
    <label class="star-rating__ico fa fa-star-o fa-lg" for="star-rating-3" title="3 out of 5 stars"></label>
    <input class="star-rating__input" id="star-rating-2" type="radio" name="rating" value="2">
    <label class="star-rating__ico fa fa-star-o fa-lg" for="star-rating-2" title="2 out of 5 stars"></label>
    <input class="star-rating__input" id="star-rating-1" type="radio" name="rating" value="1">
    <label class="star-rating__ico fa fa-star-o fa-lg" for="star-rating-1" title="1 out of 5 stars"></label>
  </div>
</div>

CSS

.star-rating{
    font-size: 0;
}
.star-rating__wrap{
    display: inline-block;
    font-size: 1rem;
}
.star-rating__wrap:after{
    content: "";
    display: table;
    clear: both;
}
.star-rating__ico{
    float: right;
    padding-left: 2px;
    cursor: pointer;
    color: #FFB300;
}
.star-rating__ico:last-child{
    padding-left: 0;
}
.star-rating__input{
    display: none;
}
.star-rating__ico:hover:before,
.star-rating__ico:hover ~ .star-rating__ico:before,
.star-rating__input:checked ~ .star-rating__ico:before
{
    content: "\f005";
}
查看更多
放我归山
5楼-- · 2020-05-14 10:12

Font Awesome and JQuery DEMO

HTML

<div>Rating Star - Function CallBack onChange</div>
<div class="well well-sm">
    <span id="rating_1" class="rating" ></span>
    <span id="result_1"></span> 
</div>
<div>Rating Star - No Editable</div>
<div class="well well-sm">
    <span id="rating_2" class="rating" data-val="2.49" data-stars='6' data-change="false"></span>
    <span>Calificaste con <b>2.49</b> de <b>6</b> Estrellas</span> 
</div>
<div>Rating Star - Tamaño - Data Html options -- Hidden input</div>
<div class="well well-sm">
   <span id="rating_3" class="rating" data-val="2.49" data-stars='10' data-input=".rating-value" data-change="true" style="font-size:35px;" >
        <input type="hidden" name="whatever3" class="rating-value" size="5"  />
   </span>
</div>
<div>Rating Star - javascript Options - input text</div>
<div class="well well-sm">
   <span id="rating_4" class="rating">
       <input type="text" name="whatever4" id="rating_val" size="1" readOnly="readOnly" />    
   </span>
    <span id="result_4"> &nbsp;&nbsp;de <b>10</b> estrellas </span> 
</div>

JavaScript

 $('#rating_1').RatingStar(
   {callback:function(val){$('#result_1').html(" &nbsp;"+val+" estrella(s).")}}
 );

 $('#rating_2').RatingStar();

 $('#rating_3').RatingStar();

 $('#rating_4').RatingStar({
     val:4.95,
     stars:10,
     input:'#rating_val',
     change:true
 });
查看更多
forever°为你锁心
6楼-- · 2020-05-14 10:17

Consider Star Ratings With Very Little CSS from CssTricks. It uses no images, no Bootstrap, no Javascript. Pure CSS and Unicode stars, see this. Supported by all browsers except IE <= 6.

Note: Javascript is not required for UI, but is required for sending data to the server and preserving the rating selection after the element loses focus.

Also take a look at Accessible star rating widget with pure CSS. It is a pure CSS as well. Uses a technique discussed here - Custom radio and checkbox inputs using CSS. It's different from the first one in a way that the rating stays selected after the element loses the focus. Unfortunately, it is supported by fewer browsers.

A quick note on CSS Selectors might be useful while reading that.

UPDATE:

Have just reread the post and clicked the link. The first link in my answer is the same as topic starter's. Apologies.

Anyway, I chose not to delete the answer, as it contains a few links which I've been very glad to find and which might be useful for others.

查看更多
Animai°情兽
7楼-- · 2020-05-14 10:22

I ended up using Raty. If you are looking for a simple and clean solution, I found this the easiest.

$('#star').raty('score');                   // Get the current score.
查看更多
登录 后发表回答