How to pass an argument to a method on a template variable in Django?

前端 未结 3 1165
长情又很酷
长情又很酷 2020-12-10 01:20

I am using the lxml library to define a variable (category) in a view. lxml provides a method .get to retrieve custom attributes. I\'d like to use it in the tem

相关标签:
3条回答
  • 2020-12-10 02:01

    You can't pass an argument to a callable attribute like this. Either pull the value in the view, or write a custom template tag to do it.

    0 讨论(0)
  • 2020-12-10 02:09

    I agree with the philosophy of separating logic from design, but there's an exception. I am currently writing a get_image(height=xxx, width=xxx) method for a model. Clearly, it should be up to the template designer to decide the size of the image as the last stage. Though I suppose the correct thing to do is write a custom tag, but why restrict?

    0 讨论(0)
  • 2020-12-10 02:20

    Here I wrote a hack to call a function and pass the arguments http://www.sprklab.com/notes/13-passing-arguments-to-functions-in-django-template/

    0 讨论(0)
提交回复
热议问题