Thursday, December 30, 2010

Show HTML in Labels for Model Choice Fields

Hi I have a situation like this

class AppearanceThemesForm(forms.Form):
page_theme =
forms.ModelChoiceField(widget=forms.RadioSelect(attrs={'title':'Select
the appropriate theme for your pages.'}),

queryset=Theme.objects.filter(section="1"),

initial=Theme.objects.get(section="1",representation="").pk,
label="Page Theme")

And my Theme model is like this:

class Theme(models.Model):
name = models.CharField(max_length=32)
image = models.ImageField(upload_to="images/themes", null=True)

def __unicode__(self):
return "%s" % (self.name)


When I render AppearanceThemesForm in my template, i would like to be
able to show a link next to the label. If a user clicks the link, the
webpage displays the image associated with Theme.

I tried putting in a custom field that extends the ModelChoiceField,
and in the label_from_instance method, tried to output the HTML, but
that gets escaped.

What is the proper way to achieve what i am trying to do?

Alternatively, how do i get access the underlying model object behind
the modelchoice field in my templates?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment