2017-04-03 9 views
-1

작은 문제가 있습니다. 즉, 클라이언트의 수를 관리자 패널의 퍼센트로 변환하고 싶습니다. 최대 부동 소수점 수는 최대 5.0입니다. 그래서 그는 5 개의 별표를 얻었고, 좋아하는 것이 없다면 0.0을 말합니다. 설립되었습니다.좋아하는 숫자를 퍼센트로 변환하는 방법, 플라스크?

평균 점수 : 속도가 예를 들어 이런 모습이 될 것입니다

appointment_upvotes = db.Table('appointment_upvotes', 
    db.Column('appointment_id', db.Integer, db.ForeignKey('appointment.id')), 
    db.Column('rate_appo_id', db.Integer, db.ForeignKey('rate_appo.id')) 
) 

class Appointment(db.Model): 
    id = db.Column(db.Integer(), primary_key=True) 
    start_time = db.Column(db.Integer(), nullable=False) 
    end_time = db.Column(db.Integer(), nullable=False) 
    timezone = db.Column(db.String(TIMEZONE_LEN_MAX), nullable=False) 
    note = db.Column(db.String()) 
    status = db.Column(db.Boolean(), default=False) 

    is_accepted = db.Column(db.Boolean(), default=False) 
    is_not_accepted = db.Column(db.Boolean(), default=True) 

    client_id = db.Column(db.Integer(), db.ForeignKey('client.id')) 
    user_id = db.Column(db.Integer(), db.ForeignKey('user.id')) 
    service_id = db.Column(db.Integer(), db.ForeignKey('service.id')) 

    rates = db.relationship('RateAppo', backref='appointment', lazy='dynamic') 

class RateAppo(db.Model): 
    id = db.Column(db.Integer(), primary_key=True) 
    rate_punctuality = db.Column(db.Integer(), default=0) 
    rate_nonconf = db.Column(db.Integer(), default=0) 
    rate_solvency = db.Column(db.Integer(), default=0) 
    text = db.Column(db.Text(), default=None) 

    appo_id = db.Column(db.Integer(), db.ForeignKey('appointment.id')) 

    def __repr__(self): 
     return "Comment: {}".format(self.text)[0:15] 

    def has_voted(self, appointment_id): 
     select_votes = appointment_upvotes.select(
       db.and_(
        appointment_upvotes.c.appointment_id == appointment_id, 
        appointment_upvotes.c.rate_appo_id == self.id 
       ) 
     ) 
     rs = db.engine.execute(select_votes) 
     return False if rs.rowcount == 0 else True 

    def vote(self, appointment_id, typevote): 
     already_voted = self.has_voted(appointment_id) 
     vote_status = None 
     if not already_voted: 
      if typevote == 'rate_punctuality': 
       db.engine.execute(
       appointment_upvotes.insert(), 
        appointment_id = appointment_id, 
        rate_appo_id = self.id 
       ) 
       self.rate_punctuality = self.rate_punctuality + 1 
       vote_status = True 
      elif typevote == 'rate_nonconf': 
       db.engine.execute(
       appointment_upvotes.insert(), 
        appointment_id = appointment_id, 
        rate_appo_id = self.id 
       ) 
       self.rate_nonconf = self.rate_nonconf + 1 
       vote_status = True 
      else: 
       db.engine.execute(
       appointment_upvotes.insert(), 
        appointment_id = appointment_id, 
        rate_appo_id = self.id 
       ) 
       self.rate_solvency = self.rate_solvency + 1 
       vote_status = True 
     db.session.commit() 
     return vote_status 

관리자 패널 내부 : 여기

model.py 4.5 '

아무튼 그가 얼마나 좋아하는지, 그가 100 개의 좋아하는 것을 말하면서, 나는 그들을 카운트로 보여주고 싶지 않다. 가치를 단지 마크까지 얻을 필요가있다. 5.0

내가 언급 한 것을 잊어 버린 또 다른 사실, 내 jinja 템플릿 내에서 작동하도록 할 수 있습니까 ?? 4.52

여력 100 %로 좋아 어쩌면 것이다 :

엄수 : 여기

는 일례이다 퍼센트 63는 그렇게 어쩌면 3.23

종일 4 개, 솔벤 시티은 3 별 등으로 표시됩니다. click me to see an example

그래서 내 템플릿 안에 내가 입력 할 수 있습니다

마지막 것은, 한 줄에 전체 통계 모든이 좋아하는의을 보여 내 패널 내부에 예를 들어 나는 이런 식으로 뭔가를 얻을 것입니다 이 같은 : 내가 위의 입력 한 내용의

<div class="col-sm-8"><h2>Average Rating: <strong> {{ "%.2f" % user.rate_appo.count }} </strong></h2> 
    <div class="table-responsive"> 
    <table class="table table-striped text-center"> 
     <thead> 
     <tr> 
      <th class="text-center">Punctuality</th> 
      <th class="text-center">Non-Conflict</th> 
      <th class="text-center">Solvency</th> 



     </tr> 
     </thead> 
     <tbody> 
     <tr> 
      <td>{{ "%.2f" % user.rate_appo.rate_punctuality.count}} </td> 
      <td>{{ "%.2f" % user.rate_appo.rate_nonconf.count}} </td> 
      <td>{{ "%.2f" % user.rate_appo.rate_solvency.count}} </td> 


     </tr> 



     </tbody> 
    </table> 
</div> 

오전 정말 확실하지, 난 그냥 시험을 단순화하기 위해 .2f % 사용 여러분 모두를 위해 더 분명하게되어주십시오.

결국 프로젝트가 진행 중이며 제안 사항을 알려 주시면 감사하겠습니다. :).

답변