显示选中元素为复选框的复选框

问题描述:

我试图让用户从核对清单的形式提供的业余爱好列表中挑选业余爱好。当他们被选中时,所选择的爱好会进入用户表。我想让我的复选框显示用户的兴趣爱好,就好像选择了兴趣爱好一样,它会被检查,如果没有检查它。用户可以通过选中或取消选中复选框来更新他们的业余爱好。
我无法以选中或未选中的形式显示用户兴趣爱好的当前状态,并从中更新用户兴趣表。显示选中元素为复选框的复选框

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如下

<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> 

目前我正在考虑让.584清单列表,所有的业余爱好列表进行比较,以参加者的爱好列表中,如果有,返回检查,如果不返回null。 我实际上在编码时遇到麻烦,不知道这是否是最好的方法。

任何帮助将不胜感激。

+0

您在这里使用哪种模板语言? –

+0

我使用的是GAE中的django – minarai

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

谢谢史蒂夫。我试过了,我得到 TemplateSyntaxError:无法解析余数:'yes'如果爱好在attendee.hobbies其他'' – minarai

+0

对不起,我认为这是在Django模板的语法,但它必须是特定于jinja2。使用这个略微丑陋的语法,“{%if user.hobbies%中的爱好} checked =”yes“{%endif%}'。 –

+0

我试过了,我得到了TemplateSyntaxError:'if'语句格式不正确。 我也试过如果出就像 {%for hobby in hobbies_list%} {%if hobby in attendee.hobbies%} {{hobby.name}}
{%endfor%} {{hobby。 }
{%endfor%} 我仍然得到相同的错误..... aaa – minarai

最简单的方法是使用表单库,如Django表单或WTForms。当您已经完成了大量的表单处理代码时,无需编写任何代码。

+0

感谢尼克,我会研究这些。 – minarai

+1

我很想知道是谁投了票,为什么。如果你认为答案是错误的,留下评论是有建设性的。 –

这应该为 “选中” 属性工作(其余是史蒂夫回答):

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

UPDATE:

我没有使用该模板引擎很长一段时间。我相信这是因为你不能在{{ }}内使用in,and,or

丑陋的解决方法是:

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

希望它能帮助。

+0

我仍然使用史蒂夫的答案得到同样的错误.. – minarai