python - Using a context processor in conjunction with Jinja template variables -


i in midst of deploying stripe , requires that payment values being passed stated in "cents" rather dollars. can handle on backend (i.e can process payment appropriate amount) in order render in stripe's ui, must convert price cents. ($400 becomes 40000 cents)

i attempting use context processor convert dollar price store in database dollars can following code in views.py file:

@buy_blueprint.context_processor def utility_processor():   def format_price(amount):     return u'{0:.0f}'.format(amount)   return dict(format_price=format_price) 

and following inserted in template.html file

course price: {{ format_price(40000) }} 

which renders 40000 - perfect.

but want like:

{% course in courses %} <p> course name: {{ course.course_name }} course price: {{ course.price }} max number of students: {{ course.max_number_students}}   remaining space: {{ course.spaces_left }} {% if course.spaces_left > 0 %} <form action="{{ url_for('buy.buy')}}" method="post">  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"   data-key={{key}}   data-amount= {{ format_price(course.price)}}   data-name="super teacher"   data-label="great  dancing"   data-description="learn dance">  </script>  {% else %}  <p>no more space</p>  {% endif %} <input type="hidden" name="course_id" value="{{course.id}}" /> </form> 

but issue that:

data-amount= {{ format_price(course.price)}} 

throws following error:

 file "/users/workhorse/flask-intro/project/buy/views.py", line 92, in format_price      return u'{0:.0f}'.format(amount)      valueerror: unknown format code 'f' object of type 'unicode' 

i know unicode issue, not sure how go fixing it

you convert number first:

def format_price(amount):     return u'{0:.0f}'.format(int(amount)) 

or perhaps:

def format_price(amount):     return u'{0:.0f}'.format(100*int(amount)) 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -