开发者

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
     开发者_StackOverflow社区 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.


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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜