Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何在jinja2或python中验证google app engine数据存储查询没有结果?_Python 2.7_Google App Engine_Jinja2_Google Cloud Datastore_Google App Engine Python - Fatal编程技术网

Python 2.7 如何在jinja2或python中验证google app engine数据存储查询没有结果?

Python 2.7 如何在jinja2或python中验证google app engine数据存储查询没有结果?,python-2.7,google-app-engine,jinja2,google-cloud-datastore,google-app-engine-python,Python 2.7,Google App Engine,Jinja2,Google Cloud Datastore,Google App Engine Python,我的问题是关于Python与Google App Engine数据存储和Jinja2的关系 我创建了一种称为Channel的类型,它有两个属性:content和userid class Channel(ndb.Model): content = ndb.StringProperty() userid = ndb.StringProperty() userid是从检索到的用户ID,用户登录时可以获取该用户ID 用户登录后,我对我的频道种类运行以下查询以检索用户的频道: channels

我的问题是关于Python与Google App Engine数据存储和Jinja2的关系

我创建了一种称为Channel的类型,它有两个属性:content和userid

class Channel(ndb.Model):
  content = ndb.StringProperty()
  userid = ndb.StringProperty()
userid是从检索到的用户ID,用户登录时可以获取该用户ID

用户登录后,我对我的频道种类运行以下查询以检索用户的频道:

channels = Channel.query(Channel.userid == user.user_id())
当用户已经创建了一个频道,并且我可以使用以下Jinja2代码向用户展示频道结果时,它可以正常工作:

{% for channel in channels %}
<p>userid: {{ channel.userid }} | content: {{ channel.content }}</p>
{% endfor %}
{%用于通道%}
userid:{{channel.userid}}内容:{{channel.content}

{%endfor%}

问题在于用户尚未创建频道。Jinja2代码不显示任何内容,甚至不显示“userid:”或“content:”部分,我想在查询未返回结果时向用户显示一条消息(每个用户只能创建一个通道),以指导用户创建新通道,但不知道如何在Python或Jinja2中验证这一点。

因为
通道
是一个迭代器,在迭代之前,你不可能知道那里有什么。但是,您也可以这样获取数据:

channels = Channel.query(Channel.userid == user.user_id()).fetch()
{% if channels %}
{% for channel in channels %}
<p>userid: {{ channel.userid }} | content: {{ channel.content }}</p>
{% endfor %}
{% else %}
<p>No channels!</p>
{% endif %}
然后,您只需检查列表是否为空,如下所示:

channels = Channel.query(Channel.userid == user.user_id()).fetch()
{% if channels %}
{% for channel in channels %}
<p>userid: {{ channel.userid }} | content: {{ channel.content }}</p>
{% endfor %}
{% else %}
<p>No channels!</p>
{% endif %}
{%if通道%}
{通道%中的通道为%1}
userid:{{channel.userid}}内容:{{channel.content}

{%endfor%} {%else%} 没有频道

{%endif%}
因为
频道
是一个迭代器,所以在对它进行迭代之前,您无法知道其中有什么。但是,您也可以这样获取数据:

channels = Channel.query(Channel.userid == user.user_id()).fetch()
{% if channels %}
{% for channel in channels %}
<p>userid: {{ channel.userid }} | content: {{ channel.content }}</p>
{% endfor %}
{% else %}
<p>No channels!</p>
{% endif %}
然后,您只需检查列表是否为空,如下所示:

channels = Channel.query(Channel.userid == user.user_id()).fetch()
{% if channels %}
{% for channel in channels %}
<p>userid: {{ channel.userid }} | content: {{ channel.content }}</p>
{% endfor %}
{% else %}
<p>No channels!</p>
{% endif %}
{%if通道%}
{通道%中的通道为%1}
userid:{{channel.userid}}内容:{{channel.content}

{%endfor%} {%else%} 没有频道

{%endif%}