Python 如何将多对多字段中的数据显示为一个tempate
我的问题类似于 给出的答案对我帮助不大 models.pyPython 如何将多对多字段中的数据显示为一个tempate,python,django,django-orm,Python,Django,Django Orm,我的问题类似于 给出的答案对我帮助不大 models.py class MacAddress(models.Model): address = models.CharField(max_length = 20,null=True,blank=True) def __unicode__(self): return self.address class UserProfile(models.Model): user = models.OneToOn
class MacAddress(models.Model):
address = models.CharField(max_length = 20,null=True,blank=True)
def __unicode__(self):
return self.address
class UserProfile(models.Model):
user = models.OneToOneField(User,primary_key=True,db_index=True)
MAC=models.ManyToManyField(MacAddress,related_name='macAddress')
def __unicode__(self):
return self.user.username
views.py
def mac(request):
context = RequestContext(request)
entries = UserProfile.objects.all()
e = {'posts' : entries }
return render_to_response('t.html',e, context)
t、 html
{% for post in posts %}
{% for p in posts.MAC.all %}
{{ p }}
{% endfor %}{% endfor %}
如何在t.html中以秒为单位显示与用户对应的mac地址列表,以便您编写
post.mac.all
。现在你正在写posts.MAC.all
,这给了你错误的结果
{% for post in posts %}
{% for p in post.MAC.all %} # You should write post.MAC.all instead posts.MAC.all
{{ p }}
{% endfor %}{% endfor %}
据我所知,您可能使用了错误的render_to_响应。我认为它应该是
return render\u to\u response(request,'watevertemplate.html',{“name\u of\u value\u you'want\u to\u call”:“value you want call in your template”})
,所以作为render\u to\u response的最后一个参数的字典应该看起来像render\u to\u response(request,'t.html',{“posts”:entries})
或类似内容。更不用说如何包含上下文了dictionary@TehTris嘿,请看看哦,好吧,我明白我为什么这么想了。我想我更习惯于只使用渲染
,为什么答案对你没有多大帮助?