1

I want to add some inputs in Django admin's search form.

I override the original template by putting a new template in the related path to origin 'admin/search_form.html'. But when I add a 'select widget' in the new template like: {{ select_widget }}. It always be rendered as <django.forms.widgets.Select object at 0x10f7560d0>. However, it is rendered as correct html if I invoke its render() method.

How can I get the widget be rendered as correct html?

papalagi
  • 710
  • 2
  • 8
  • 20

1 Answers1

0

Try this:

{{ select_widget.render }}

From Django Docs:

Technically, when the template system encounters a dot, it tries the following lookups, in this order:

  • Dictionary lookup
  • Attribute lookup
  • Method call
  • List-index lookup

What you need here is the 3rd - method call.

Tisho
  • 8,320
  • 6
  • 44
  • 52
  • Thanks. But render takes two arguments: name, value. How can I pass them to it? – papalagi Jul 10 '12 at 17:09
  • `{{ select_widget.render arg1 arg2 }}` – Tisho Jul 10 '12 at 17:09
  • `{{ select_widget.render None None }}` will lead to `TemplateSyntaxError` with message `Could not parse the remainder: ' None None' from 'select_widget.render None None'` – papalagi Jul 10 '12 at 17:12
  • Ops, sorry. Here is an example of passing an argument to method: http://www.diegor.it/2011/04/18/howto-passing-arguments-to-functions-in-django-template/ – Tisho Jul 10 '12 at 17:15