I have an awesome widget to display information, and I want to use it in a template but without using a form.
My template is used only to display info, there is no form to be submited. How can I render this widget in a template without using a form. I have to take into account that the widget is feed with some user data. Can I use a template tag for this?
thanks!
Implement the widget's render
method in you custom Widget class.
render(self, name, value, attrs=None)
The render method is responsible for the html representation of the widget.
E.g the render method of TextInput
returns this piece of html:
>>> name = forms.TextInput(attrs={'size': 10, 'title': 'Your name',})
>>> name.render('name', 'A name')
u'<input title="Your name" type="text" name="name" value="A name" size="10"/>'
You can then add the returned value of the render method to the context of a view.
def my_view(request):
widget_html = MyCustomWidget.render(...)
return render_to_response(..., {'widget_html': widget_html})
Now you can display the widget in your template using:
{{ widget_html }}
You could also write a templatetag as @ablm suggested.