我正在编写一个ToDo列表应用程序,以帮助自己开始使用Python。该应用程序在GAE上运行,我将待办事项存储在数据存储区中。我想将每个人的物品展示给他们,也要单独展示给他们。问题是该应用程序当前向所有用户显示所有项目,因此我可以看到您写的内容,也可以看到我写的内容。我以为将我的todo.author对象转换为字符串并查看它是否与用户名匹配是一个不错的开始,但是我不知道该怎么做。这就是我的main.py中的内容... user = users.get_current_user()if user: nickname = user.nickname() todos = Todo.all() template_values = {'nickname':nickname, 'todos':todos}...def post(self): todo = Todo() todo.author = users.get_current_user() todo.item = self.request.get("item") todo.completed = False todo.put() self.redirect('/')在我的index.html中,本来是这样的:<input type="text" name="item" class="form-prop" placeholder="What needs to be done?" required/>... <ul>{% for todo in todos %} <input type="checkbox"> {{todo.item}} <hr />{% endfor %}</ul>但我只想向创建它们的用户显示项目。我想尝试{% for todo in todos %} {% ifequal todo.author nickname %} <input type="checkbox"> {{todo.item}} <hr /> {% endifequal %}{% endfor %}无济于事。列表变成空白。我以为是因为todo.author不是字符串。我可以将值读取为字符串,还是可以将对象转换为String?谢谢!
3 回答
鸿蒙传说
TA贡献1865条经验 获得超7个赞
在python中,该str()方法类似于toString()其他语言中的方法。称为传递对象以将其转换为字符串作为参数。在内部,它调用__str__()参数对象的方法以获取其字符串表示形式。
但是,在这种情况下,您正在比较UserProperty数据库中的作者,该作者的类型users.User与昵称字符串相同。您将需要在模板中比较nickname作者的属性todo.author.nickname。
桃花长相依
TA贡献1860条经验 获得超8个赞
在Python中,我们可以使用__str__()方法。
我们可以像这样在我们的类中重写它:
class User: firstName = '' lastName = '' ... def __str__(self): return self.firstName + " " + self.lastName
并且在跑步时
print(user)
它将调用该函数__str__(self) 并打印firstName和lastName
千巷猫影
TA贡献1829条经验 获得超7个赞
str() 是等效的。
但是,您应该过滤查询。目前,您的查询是all()Todo。
todos = Todo.all().filter('author = ', users.get_current_user().nickname())或者
todos = Todo.all().filter('author = ', users.get_current_user())取决于您在Todo模型中定义的作者。AStringProperty或UserProperty。
注意nickname是一种方法。您正在传递方法,而不是模板值中的结果。
添加回答
举报
0/150
提交
取消
