[英]Display object for Django Context Processors
I'm working to add some business details as a context processor within an app called Business
.我正在努力在名为Business
的应用程序中添加一些业务详细信息作为上下文处理器。 I've included in it a folder titled templatetags
for the __init__.py
and business_tags.py
files.我在其中包含了一个名为__init__.py
和business_tags.py
文件的templatetags
__init__.py
的文件夹。 My problem is while the context processor shows a result, I am unable to display the results as a loop.我的问题是上下文处理器显示结果时,我无法将结果显示为循环。
business_tags.py file: business_tags.py 文件:
from django import template
register = template.Library()
from django.contrib.auth.models import User
from ..models import Businessprofile
@register.simple_tag
def my_biz(request):
current_user = request.user.id
biz = Businessprofile.objects.filter(owner=current_user)
return biz
On my view file I am currently made a for/endfor for the loop:在我的视图文件中,我目前为循环创建了一个 for/endfor:
<!--content-->
{% load business_tags %}
{% my_biz request %}
{% for biz in my_biz %}
{{ biz }}
{% endfor %}
<!--end content-->
How do I display the results of the context processor as a for loop?如何将上下文处理器的结果显示为 for 循环?
Your simple tag is just returning the object (it is not saved in "my_biz"), you need to save the return in a variable in this way:你的简单标签只是返回对象(它没有保存在“my_biz”中),你需要以这种方式将返回值保存在一个变量中:
{% my_biz request as my_biz_var %}
{% for biz in my_biz_var %}
{{ biz }}
{% empty %}
my_biz_var is empty
{% endfor %}
Aditional note : as is pointed by Daniel Roseman, what you are doing is not a context processor but a simple tag.附加说明:正如 Daniel Roseman 所指出的,您所做的不是上下文处理器,而是一个简单的标签。
context procesors: https://docs.djangoproject.com/en/1.11/ref/templates/api/#django.template.RequestContext上下文处理器: https ://docs.djangoproject.com/en/1.11/ref/templates/api/#django.template.RequestContext
Simple tags:简单标签:
https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/#simple-tags https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/#simple-tags
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.