在 django 模板中访问 couchdb 的 UUID
我对 Couchdb 和 Django 有点陌生,并试图扩展 couchdbkit 提供的默认示例(问候语)。在使用现有示例制作基本的 CRUD 示例时,我无法访问 django 模板中的 couchdb 文档 id
当我运行类似这样的内容时,
greetings = Greeting.view('greeting/all', descending=False)
for g in greetings:
print g._id
将其传递给模板时
return render("home.html", {
"form": form,
"greet": greet,
"greetings": greetings
}, context_instance=RequestContext(request))
它打印得很好,尽管当使用使用提到的类似循环 上面我可以访问
{% for g in greetings %}
<tr>
<td>{{ g.date|timesince }} </td>
<td>{{ g.author }} </td>
<td>{{ g.content }}</td>
</tr>
{% endfor %}
作者和内容,但是当我介绍 {{ g._id }}
我在 / 处收到错误 TemplateSyntaxError 变量和属性可能不以下划线开头:“g._id”
有人可以解释一下我如何在 django 模板中访问唯一的 couchdb id,因为我知道我可以在 view.py 中访问“_id”,这不是问题对于 couchdbkit,需要关注的是 view.py 中可能将 _id 转换为 id 或其他内容的解决方案。
如前所述,我是 django 的新手,因此这个问题本身可能有点愚蠢。
PS:我正在使用 coucdb 1.0.2、django 1.3.0 和 python 2.6
谢谢。
I am kinda new to Couchdb and Django and was trying to extend the default sample (greetings) provided with couchdbkit. while making a basic CRUD example using the existing example, I wasn't able to access the couchdb's document id in the django template
When I run something like this
greetings = Greeting.view('greeting/all', descending=False)
for g in greetings:
print g._id
It prints fine, although when passed it to the template using
return render("home.html", {
"form": form,
"greet": greet,
"greetings": greetings
}, context_instance=RequestContext(request))
Using the similar loop as mentioned above I can access
{% for g in greetings %}
<tr>
<td>{{ g.date|timesince }} </td>
<td>{{ g.author }} </td>
<td>{{ g.content }}</td>
</tr>
{% endfor %}
The author and content, but when I introduced
{{ g._id }}
I get an error TemplateSyntaxError at /
Variables and attributes may not begin with underscores: 'g._id'
can somebody please explain how do i access the unique couchdb's id in django templates, since I know I can access the "_id" in view.py it isn't a problem with couchdbkit, the concern is a work around probably in view.py to maybe convert _id to id or something.
As mentioned, I am a newbie to django, hence maybe the question might be a little silly in itself.
PS: I am using coucdb 1.0.2, django 1.3.0 and python 2.6
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模板错误的原因很简单:Django 不允许访问以下划线开头的对象的属性。我不知道其原因,但按照约定,以下划线开头的属性在 Python 中是“私有”的。所以我猜这是 Django 强制执行的,目的是迫使开发人员不要做这样的事情,因为他们的风格很糟糕。
首先,您应该确保没有其他方法可以访问 id(例如
get_id()
或其他方法)。如果没有,您可以解决您的问题:将_id
复制到每个问候语的新属性。现在,greetings 中的每个实例都有一个新属性
couchID
,您可以在其中访问_id
的值。这项工作之所以有效,是因为 Python 的动态特性允许您在运行时修改任何内容 - 您甚至可以创建或删除属性。在您看来,您可以简单地执行以下操作:
The reason for the template error is simple: Django doesn't allow you to access attributes of objects that begin with a underscore. The reason for this is unknown to me, but by convention attributes that begin with an underscore are "private" in Python. So I guess this is enforced by Django to force the developers to don't do such things as their are bad style.
Frist, you should make sure, that there is no other way to access the id (like
get_id()
or something). If not you can work around yout problem: Copy_id
to a new attribute for each greeting.Now each instance in greetings has a new attribute
couchID
where you can access the value of_id
. This work because Pythons dynamic nature allows you to modify anything at runtime - you can even create or remove attributes.In your view you can simple do:
另一种选择是使用 templatetag 访问它
然后在 html 代码中您可以使用以下方式访问它:
它可以节省存储更多变量,并且您不必为每个要访问它的对象执行此操作,这使得它更加干燥。
Another option is accessing it using a templatetag
Then in your html code you can access it using:
It saves storing more variables and you won't have to do it for each object you want to access it, which makes it DRYer.