Appengine - 如何获取实体并显示值

发布于 2024-09-19 06:26:02 字数 3345 浏览 3 评论 0原文

我的项目遇到了麻烦。我有 2 个型号

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

问题: 我知道数据是正确放置的。我使用带有以下网址的 SDK 控制台: http://localhost:8080/_ah/admin/datastore 并且实体已正确创建。我不知道我缺少什么来检索已经放入

我的模板的数据:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待我真够勒 2024-09-26 06:26:02

你的代码有点混乱。
使用组织得更好的代码,调试通常会更容易。

无论如何,废话已经说够了。
您将数据存储查询的结果分配给 persons...

persons = person.fetch(limit = 1)

...但是在模板中您使用 person

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

很难判断这是否是您的唯一的问题(我非常怀疑),但也许你可以尝试解决这个问题并回复我们。祝你好运。

另外:您可以简单地使用 文档

get() 意味着“限制”为 1。最多
1 个结果取自
数据存储。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文