views:

54

answers:

1

I'm having trouble with my project. I have 2 models

class UserPrefs(db.Model):
    user = db.UserProperty()
    name = db.StringProperty()

class Person(db.Model):
    name = db.StringProperty()
    phone = db.PhoneNumberProperty()
    userPrefs = db.ReferenceProperty(UserPrefs)

class PersonHandler(webapp.RequestHandler): 
    def get(self): 
        users.get_current_user user = () 
        if user: 
            greeting = ...... 
        else: 
            greeting = ......

        if self.request.GET.has_key ('id'): 
            id = int (self.request.get ['id']) 
            person = models.Person.get = (db.Key.from_path ('Person', id)) 

            path = os.path.join (os.path.dirname (__file__), 'templates / doStuff.html') 
            self.response.out.write (template.render (path, locals (), debug = True)) 

        def post (self): 
            if self.request.get ('Person'): 
                id = int (self.request.get ('Person')) 
                person = models.Person.get (db.Key.from_path ('Person', id)) 
            else: 
                person= models.Person = () 

            data = forms.PersonForm date = (data = self.request.POST) 
            if data.is_valid (): 
                if self.request.get ('photo'): 
                    Person.foto db.Blob = (self.request.get ('photo')) 

                person.nome self.request.get = ('name') 
                person.apelido self.request.get = ('name') 
                person.unidade self.request.get = ('unit') 

                person.put () 
                self.redirect ('/ doSomeStuff') 
            else: 
                self.redirect ('doOtherStuff')

To See the data in database i use this handler:
class SeePersonHandler (webapp.RequestHandler): 
    def get (self): 
        users.get_current_user user = () 
        if user: 
            greeting = ...... 
        else: 
            greeting = ......

        person= db.Query(models.Pocente)
        persons = person.fetch(limit = 1) 

        path = os.path.join(os.path.dirname(__file__), 'templates/SeeStuff.html')
        self.response.out.write(template.render(path, locals(), debug = True))

Question: I knows that the data is put corectly. I used the SDK Console with this url: http://localhost:8080/_ah/admin/datastore and the entity is created correctly. I don´t know what i am missing to retrieve the dadta already put

My Template:

{% if user %}
  {% if person%}
    <table align="center">
    <tbody>
    <tr>
      <td><input type="button" value="Criar Pessoa" onclick="redirect(3)" /></td>
    </tr>
    </tbody>
    </table>
    <table align="center">
    <tbody>
    <tr>
      <td colspan="2"><center><strong><p>O meu Curriculum Vitae</p></strong></center></td>
    </tr>
    <tr>
      <td>Nome: </td>
      <td>{{ person.name}}</td>
    </tr>
    <tr>
      <td>Apelido: </td>
      <td>{{ person.phone}}</td>
    </tr>
    <tr>
      <td></td>
      <td>
        <input type ="button" value="Editar" onclick="editarCv({{ person.key.id }})" />
      </td>
    </tr>
    </tbody>
    </table>
  {% endif %}
{% endif %}
A: 

Your code is a bit disorganised.
Debugging is generally easier with better-organised code.

Anyway, enough of the trash-talking.
You're assigning the result of a datastore query to persons...

persons = person.fetch(limit = 1)

...but then in your template you use person:

<tr>
  <td>Nome: </td>
  <td>{{ person.name}}</td>
</tr>
<tr>
  <td>Apelido: </td>
  <td>{{ person.phone}}</td>
</tr>

It is difficult to tell if this is your only problem (I highly doubt it), but perhaps you can try fixing that and get back to us. Best of luck to you.

Aside: instead of .fetch(limit=1) you can simply use .get() as mentioned in the documentation:

get() implies a "limit" of 1. At most 1 result is fetched from the datastore.

Adam Bernier
Thanks Adam. It worked. Now when i try to edit the information, i get this error: File "C:\Users\hp\workspace\AnApp\src\main.py", line 173, in get id = int(self.request.get['id'])TypeError: 'instancemethod' object is unsubscriptable
Ana Ferreira
What is weird is that the error referes to a handler that i use to put the info like: if self.request.GET.has_key('id'): id = int(self.request.get['id']) list_Person = models.Person.get(db.Key.from_path('Person', id))
Ana Ferreira
Sorry for ask this. I was dumb. My mistake was get instead of GET.
Ana Ferreira