minarai
minarai

Reputation: 41

Displaying checkbox with selected element as checked

I'm trying to let a user pick hobbies from list of hobbies that is provided in the form of checklist. When they are chosen, chosen hobbies goes into the user table. I would like for my checkbox to display the user's hobbies, as if the hobbies is selected, it is checked and if not its not checked. User can update their hobbies by checking or unchecking the checkbox.
I'm having trouble displaying the current state of user hobbies in checked or unchecked form and updating the user hobbie table from it.

class InfoPage(BasePage):
    title = 'One Macnica'

  def get(self):
    self.write_page_header()

    hobbies_list = Hobby.all().fetch(100)



    template_values = {"hobbies_list": hobbies_list}
    path = os.path.join(os.path.dirname(__file__), 'templates')
    path = os.path.join(path, 'hobby.html')
    self.response.out.write(template.render(path, template_values))
    self.write_page_footer()

  def post(self):

    self.write_page_header()

    hobbies_list = Hobby.all().fetch(100)


    if self.request.get("hobby"):
      hobby_name = self.request.get('hobby')
      new_hobby = Hobby(name=hobby_name.strip(), key_name = hobby_name.strip())

      #fetch attendee and add new hobby
      attendee_query = Attendee.gql('WHERE email = :1',
          users.get_current_user().email())
      attendee = attendee_query.fetch(1)
      attendee = attendee.pop()
      hobbies = attendee.hobbies
      hobbies.append(new_hobby.key())
      attendee.hobbies = hobbies
      #eliminate the dupliate
      hobbies = list(set(hobbies))
      attendee.hobbies = hobbies
      attendee.put()



    template_values = {"hobbies_list": hobbies_list}
    path = os.path.join(os.path.dirname(__file__), 'templates')
    path = os.path.join(path, 'hobby.html')
    self.response.out.write(template.render(path, template_values))
    self.write_page_footer()

hobby.html is the following

<h1 id="header">One Macnica</h1>
<p>Welcome to One Macnica, a place dedicated to connecting Macnica as 1.
</p>
<form name="hobby" action="/hobby" method="post">


{% for hobby in hobbies_list %}
   <input type="checkbox" name = "{{hobby.name}}"/>{{hobby.name}}<br/>
{% endfor %}


<input type="submit" value="Select Hobbies">
</form>

i'm currently thinking of making a list for hobby checklist which compares all the hobby list to attendee's hobby list and if it there, return checked and if not return null. i'm having trouble actually coding this and dont know if this is the best way.

any help would be appreciated it.

Upvotes: 0

Views: 1403

Answers (3)

Steven Kampen
Steven Kampen

Reputation: 631

{% for hobby in hobbies_list %}
  <input type="checkbox" {% if hobby in user.hobbies %}checked="yes"{% endif %} name = "{{hobby.name}}"/>{{hobby.name}}<br/>
{% endfor %}

Upvotes: 1

Eren G&#252;ven
Eren G&#252;ven

Reputation: 2374

This should work for "checked" attribute (rest is as Steve answered):

checked={{ hobby in user.hobbies and "checked" or "" }}

UPDATE:

I haven't used that template engine for a long time. I believe it's because you can't use in,and,or inside {{ }}.

Uglier workaround would be:

checked={% if hobby in user.hobbies %}"checked"{% else %}""{% endif %}

Hope it helps.

Upvotes: 1

Nick Johnson
Nick Johnson

Reputation: 101149

The easiest way to do this is to use a forms library, like Django forms or WTForms. There's no need to write large amounts of form handling code when it's already been done for you.

Upvotes: 1

Related Questions