0

let's say I define a function in my model to return a formatted value like:

def formatMoney(self):
  return '$' + self.money

How can I use this on my view?

Well I guess I expressed myself poorly. I'm trying to use it on the template file not in the view class as I'm iterating in the template file.

1 Answer 1

4

call the function from the instance of your model, same way you would call a function on an instance outside of a view.

# views.py

def your_view(request):
    instance = YourModel.objects.get(pk=234)
    instance.formatMoney()
    render(request, 'your_template.html', {'instance': instance})

# your_template.html
  {{ instance.formatMoney }}

To call methods inside of djangos template language, omit the ()

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I added more info to my question, and now that I searched for template instead of view I found answers, thank you.
Hi, how do it with queryset? For example, I want to use get_member_rating then order_by member_rating in queryset on views

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.