basic problem: i need to create real customized templates for my django forms. That's it. I'm used to style the forms based on django's output. This time is different, the html/css template is already done (months before i was hired) and i cannot modify it, so i need django to output exactly that html code.
I've read a lot throu django docs, and i found a lot, but i have to admit, i couldn't put bits and pieces togheter, with order, to do what i need to do.
Now, with CheckboxSelectMultiple the html output is this:
<ul>
<li><input type='checkbox' ...></li>
...
</ul>
What i actually need to do is create something like CustomCheckboxSelectMultiple to output exactly this template:
<ul class="li开发者_Go百科st">
<li class="list-item"><input class="checkbox" type="checkbox" id="..." /><label class="label" for="">...</label></li>
...
</ul>
And so on for other types of form widgets. It's the only way i can think of to create my forms with that particular layout they gave me.
I'm actually stuck on this, i cannot seem to be able to put everything togheter to start coding my forms. How can i manage this? If you have any example too, it will be great! Otherwise, just point me towards the right direction please... sadly, looking in the docs, as i said before, didn't actually help me
Thanks all in advance!
you should subclass the CheckboxSelectMultiple class and override the render method as in:
class CustomCheckboxSelectMultiple (CheckboxSelectMultiple):
"""
A custom CheckboxSelectMultiple Widget that render specific html
"""
def __init__(self, attrs={}):
super(CustomCheckboxSelectMultiple, self).__init__(attrs)
def render(self, name, value, attrs=None):
#Here the custom code
See original CheckboxSelectMultiple source for inspiration of how to make your new method.
After that your signal the overriding in your Form
class MyCustomForm(forms.Form):
formfield_overrides = {
models.CheckboxSelectMultiple : {'widget': CustomCheckboxSelectMultiple }
}
And now all your MyCustomForm will use your specific widget for all CheckboxSelectMultiple in it.
精彩评论