user2354910
user2354910

Reputation: 183

Is there a way to replace label text with image in Django form?


I would like to have something in forms.py like:

class myForm(forms.Form):
    a = forms.IntegerField()
    b = forms.CharField(label="my image.jpg here")

And in html source code it should looks like:
    <label for="test"><img src="{% static "images/image.jpg" %}"></label>

Is that possible?


Regards,
yang

Upvotes: 1

Views: 429

Answers (1)

vadersaw
vadersaw

Reputation: 1

I think you have a few options...

You could set your label to label="" (Untested, unsure if it would work... might consider using {}.format to get your string set).

Redefine a built in method for displaying the label. You could modify as_p(), as_ul(), etc or redefine label_tag(). The source code is here where you can investigate. I think redefining label_tag is your best choice.

Upvotes: 0

Related Questions