Re: Django Models: How to allow users to select an image from a choice of 3 pictures

2,690 views
Skip to first unread message

Ivan Ivanov

unread,
Jul 15, 2012, 10:52:00 PM7/15/12
to django...@googlegroups.com
На Sun, 15 Jul 2012 18:26:02 -0700 (PDT)
croberts <camden...@gmail.com> написа:

> I am familiar with the use of Field.choices for text.
>
> However, I'd like the same option for images. Is this possible? I
> want users to select one image from a list of 3 and be able to save
> this. They can later come back in and change it if they wish. I
> know that in order to save this, it must be in Models.
>
> The idea:
>
> Each day, the has the option of selecting an image: happy face, sad
> face, smiley face of how they are feeling. Their selection is
> saved. They can go in later to change it if they wish.
>
> ImageField doesn't seem to do this. It seems to only allow for
> uploaded images as opposed to a choice.
>
Use Choice field with image in the option. You do not need FIleField,
couse you don't operate with files at all. You just want to put an
image in a select.

There are several ways to achieve this:
background image (pure html+css)
http://binnyva.blogspot.com/2006/01/icons-for-select-menu-options-in.html

Jquery UI;
or something like this:
http://www.marghoobsuleman.com/jquery-image-dropdown

Melvyn Sopacua

unread,
Jul 24, 2012, 11:20:35 AM7/24/12
to django...@googlegroups.com
On 16-7-2012 3:26, croberts wrote:
> I am familiar with the use of Field.choices for text.

Then that's all you need. Let the template handle the image rendered:
EMO_CHOICES = (
('smiley_sad.png', 'sad sad emo'),
('smiley_neutral.png', 'ask me later'),
('smiley_happy.png', 'happy happy joy joy'),
)

in template:
{% load static %}
...
<img src="{% static "images/" %}{{model.field.choicefield}}" .../>

And of course you can dress it up as Ivan showed, but the basic
principles are the same.

--
Melvyn Sopacua
Reply all
Reply to author
Forward
0 new messages