Python 是否具有等效的 toString(),我可以将类转换为 String 吗? [英] Does Python have a toString() equivalent, and can I convert a class to String?

查看:50
本文介绍了Python 是否具有等效的 toString(),我可以将类转换为 String 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个待办事项列表应用程序来帮助自己开始使用 Python.该应用程序在 GAE 上运行,我将待办事项存储在数据存储中.我想向他们展示每个人的物品,并且单独展示给他们.问题是该应用程序当前向所有用户显示所有项目,因此我可以看到您写的内容,您也可以看到我写的内容.我认为将我的 todo.author 对象转换为字符串并查看它是否与用户名匹配会是一个好的开始,但我不知道该怎么做.

I'm writing a ToDo list app to help myself get started with Python. The app is running on GAE and I'm storing todo items in the Data Store. I want to display everyone's items to them, and them alone. The problem is that the app currently displays all items to all users, so I can see what you write, and you see what I write. I thought casting my todo.author object to a string and seeing if it matches the user's name would be a good start, but I can't figure out how to do that.

这是我在 main.py 中的内容

This is what I have in my 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 中,我原来有这个:

In my index.html I had this originally:

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

但我只想向创建它们的用户显示项目.我想试试

but I'd like to display items only to the user who created them. I thought of trying

{% for todo in todos %}
    {% ifequal todo.author nickname %}
  <input type="checkbox"> {{todo.item}} <hr />
    {% endifequal %}
{% endfor %}

无济于事.列表变成空白.我认为这是因为 todo.author 不是字符串.我可以将值作为字符串读出,还是可以将对象转换为字符串?

to no avail. The list turns up blank. I assumed it is because todo.author is not a string. Can I read the value out as a string, or can I cast the object to String?

谢谢!

这是我的 Todo 类

Here is my Todo class

class Todo(db.Model):
    author = db.UserProperty()
    item = db.StringProperty()
    completed = db.BooleanProperty()
    date = db.DateTimeProperty(auto_now_add=True)

将我的作者更改为 StringProperty 会产生负面影响吗?也许我可以完全放弃投射.

Will changing my author to a StringProperty effect anything negatively? Maybe I can forgo casting altogether.

推荐答案

在 python 中,str() 方法 类似于其他语言中的 toString() 方法.这称为传递对象以转换为字符串作为参数.它在内部调用参数对象的 __str__() 方法来获取其字符串表示.

In python, the str() method is similar to the toString() method in other languages. It is called passing the object to convert to a string as a parameter. Internally it calls the __str__() method of the parameter object to get its string representation.

但是,在这种情况下,您正在比较数据库中的 UserProperty 作者,该数据库类型为 users.User 和昵称字符串.您需要将作者的 nickname 属性与模板中的 todo.author.nickname 进行比较.

In this case, however, you are comparing a UserProperty author from the database, which is of type users.User with the nickname string. You will want to compare the nickname property of the author instead with todo.author.nickname in your template.

这篇关于Python 是否具有等效的 toString(),我可以将类转换为 String 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆