Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango: customize form in template according to field attribute
    primarykey
    data
    text
    <p>I've got the Category model and SearchForm form shown below. I'd like to do 2 things in my template:</p> <p>-to separate in the form the Category instances having a given type to be able to apply a specific style to them in my CSS</p> <p>-to show the hierarchy of my category instances</p> <p>Basically I need to access the Category's parent and style in my template or to modify the rendering of my form. How can I do this? thanks Jul</p> <p><strong>Category model</strong></p> <pre><code>CATEGORY_TYPE = [ (1, 'region'), (2, 'type'), ] class Category(models.Model): parent = models.ManyToManyField('self', symmetrical=False, null=True, blank=True) type = models.PositiveSmallIntegerField(choices=CATEGORY_TYPE) class Translation(multilingual.Translation): name = models.CharField(max_length=100, unique=True) class Meta: verbose_name_plural = 'Categories' def __unicode__(self): return "%s" %(self.name) </code></pre> <p><strong>SearchForm class</strong></p> <pre><code>class SearchForm(forms.Form): query = forms.CharField(max_length=100, required=False) price_range = forms.IntegerField(widget=forms.Select(choices = PRICE_RANGES_AND_EMPTY), required=False) def __init__(self, *args, **kwargs): super(SearchForm, self).__init__(*args, **kwargs) self.fields['country'] = forms.ModelChoiceField(queryset=Country.objects.all().order_by('name'), empty_label='All', required=False) self.fields['category'] = forms.ModelMultipleChoiceField(queryset=Category.objects.all().order_by('name'), widget=forms.CheckboxSelectMultiple(), required=False) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload