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条回答
Bombasti
2楼-- · 2020-05-14 10:22
var $star_rating = $('.rating .star');

var SetRatingStar = function() {
  return $star_rating.each(function() {


var puan = document.formname.whatever.value;



    if ( parseInt($(this).data('rating')) <= puan) {
      return $(this).removeClass('star').addClass('star filled');
    } else {
      return $(this).removeClass('star filled').addClass('star');
    }
  });
};

$star_rating.on('click', function() { 

  document.formname.whatever.value=$(this).data('rating');

  return SetRatingStar();
});



.rating{unicode-bidi:bidi-override;direction:rtl;font-size:10px;}
.rating span.star{font-family:FontAwesome;font-weight:normal;font-style:normal;display:inline-block}
.rating span.star:hover{cursor:pointer}
.rating span.star:before{content:"\f006";padding-right:5px;color:#999}
.rating span.star:hover:before,.rating span.star:hover~span.star:before{content:"\f005";color:#e3cf7a}
.rating span.star.filled {}
.rating span.star.filled:before{content:"\f005";color:#e3cf7a; }



<span class="rating" style="font-size:20px;">
<span class="star" data-rating="5"></span><span class="star" data-rating="4"></span><span class="star" data-rating="3"></span><span class="star" data-rating="2"></span><span class="star" data-rating="1"></span></span>
查看更多
Summer. ? 凉城
3楼-- · 2020-05-14 10:23

This is the best plugin for star rating if you are using bootstrap:

  • 2Kb minified
  • Use Bootstrap Glyphicons
  • No extra CSS
  • You just have to add class="rating" to the input

Github Project

查看更多
放我归山
4楼-- · 2020-05-14 10:28

You may check my Bootstrap JQuery Star rating plugin I created with various configurable options (demos for scenarios are included within). It uses CSS3 as much as possible, but also uses JQuery (if you are ok with that). You can get the star rating numbers OR set it easily through the plugin methods.

Uses Bootstrap 3.x glyphicons, includes RTL support, supports plugin events and methods. The plugin also supports any fractionally filled stars. You can refer the source here.

If you are keen on using Font-Awesome, you can override the plugin CSS to use Font-Awesome instead of Bootstrap Glyphicons in your project.

查看更多
够拽才男人
5楼-- · 2020-05-14 10:28

Have a look at django-ratings - that's what you're looking for.

查看更多
对你真心纯属浪费
6楼-- · 2020-05-14 10:31

I did not see a simple answer using only bootstrap glyphicons and jquery. I am sure other people have come here looking for a quick copy and paste so I just wrote one.

$(function(){
    $('.rating-select .btn').on('mouseover', function(){
        $(this).removeClass('btn-default').addClass('btn-warning');
        $(this).prevAll().removeClass('btn-default').addClass('btn-warning');
        $(this).nextAll().removeClass('btn-warning').addClass('btn-default');
    });

    $('.rating-select').on('mouseleave', function(){
        active = $(this).parent().find('.selected');
        if(active.length) {
            active.removeClass('btn-default').addClass('btn-warning');
            active.prevAll().removeClass('btn-default').addClass('btn-warning');
            active.nextAll().removeClass('btn-warning').addClass('btn-default');
        } else {
            $(this).find('.btn').removeClass('btn-warning').addClass('btn-default');
        }
    });

    $('.rating-select .btn').click(function(){
        if($(this).hasClass('selected')) {
            $('.rating-select .selected').removeClass('selected');
        } else {
            $('.rating-select .selected').removeClass('selected');
            $(this).addClass('selected');
        }
    });
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="rating-select">
    <div class="btn btn-default btn-sm"><span class="glyphicon glyphicon-star-empty"></span></div>
    <div class="btn btn-default btn-sm"><span class="glyphicon glyphicon-star-empty"></span></div>
    <div class="btn btn-default btn-sm"><span class="glyphicon glyphicon-star-empty"></span></div>
    <div class="btn btn-default btn-sm"><span class="glyphicon glyphicon-star-empty"></span></div>
    <div class="btn btn-default btn-sm"><span class="glyphicon glyphicon-star-empty"></span></div>
</div>

To set the default value from the DB using django template, for each star do:

<div class="btn btn-{% if rate.value > 0 %}warning{% else %}default{% endif %}{% if rate.value == 1 %} selected{% endif %} btn-sm"><span class="glyphicon glyphicon-star-empty"></span></div>
查看更多
登录 后发表回答