Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

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 to cast 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.

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('/')

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

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?

Thanks!

1 Answer

0 votes
by (106k points)
edited by

Python has an str() method which 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.

To know more about this you can have a look at the following video tutorial:-

Interested to learn more about Python? Come & join our Python online course

Browse Categories

...