查询django模板中的很多字段 [英] Querying Many to many fields in django template

查看:129
本文介绍了查询django模板中的很多字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能不是相关的,但只是想问一下,如果一个对象从视图传递到模板,在模板中我可以查询很多许多领域



型号代码:

  class Info(models.Model )
xls_answer = models.TextField(null = True,blank = True)


类上传(models.Model):
access = models.IntegerField )
info = models.ManyToManyField(Info)
time = models.CharField(max_length = 8,null = True,blank = True)
error_flag = models.IntegerField()

def __unicode __(self):
return self.access

视图: / p>

  // obj_Arr包含obj_Arr中objs上传
的所有对象:
logging.debug(objs .access)
logging.debug(objs.time)


返回render_to_response('upload / new_index.html',{'obj_arr':obj_Arr' load_flag':2})

在模板中,可以解码多对多字段,因为我们是传递对象



谢谢..

解决方案

一般来说,你可以通过django模板系统中的路径跟踪任何属性或方法调用,而不带任何参数。



对于上述视图代码,如

  {%for objs in obj_arr%} 
{%for objs.answers.all%}
{{answer.someattribute}}
{%endfor%}
{%endfor%}

应该做你所期待的。



(我不太清楚您的代码示例中的具体细节,但希望这将照亮您可以通过模板获得什么)


This may not be relevant but just wanted to ask,

IF an object is passed from views to template and in the template will i be able to query many to many fields

Models code:

  class Info(models.Model):
     xls_answer  = models.TextField(null=True,blank=True)


  class Upload(models.Model):
     access = models.IntegerField()
     info = models.ManyToManyField(Info)
     time = models.CharField(max_length=8, null=True,blank=True)
     error_flag = models.IntegerField()

     def __unicode__(self):
        return self.access

Views:

         // obj_Arr  contains all the objects of upload
        for objs in obj_Arr:
           logging.debug(objs.access)
           logging.debug(objs.time)


        return render_to_response('upload/new_index.html', {'obj_arr': obj_Arr , 'load_flag' : 2})

In template is it possible to decode the many to many field since we are passing the object

Thanks..

解决方案

In general, you can follow anything that's an attribute or a method call with no arguments through pathing in the django template system.

For the view code above, something like

{% for objs in obj_arr %}
{% for answer in objs.answers.all %}
  {{ answer.someattribute }}
{% endfor %}
{% endfor %}

should do what you're expecting.

(I couldn't quite make out the specifics from your code sample, but hopefully this will illuminate what you can get into through the templates)

这篇关于查询django模板中的很多字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆