打印仅与django中的外键相关的字段
Printing fields only related to foreignkey in django
我试图只打印与我的模型的外键关联的条目。我有一系列列表,我想打印列表名称和该列表下的所有项目。我当前的问题是每个列表都有一个 div 生成,这很好,但是与每个列表关联的每个项目也在 div 内部打印出来,而不仅仅是与关联的项目列表名称。
观看次数
def control_panel(request, username):
context = RequestContext(request)
if username == request.user.username:
if request.user.is_authenticated():
user = User.objects.get(username=username)
lists = request.user.newlist_set.all()
listitems = request.user.newlistitem_set.all().filter(list_name = lists)
return render_to_response('controlpanel.html', {'lists': lists,'listitems':listitems,}, context)
else:
return render_to_response('login.html', {}, context)
else:
return render_to_response('controlpanel.html', {}, context)
型号
from django.db import models
from django.contrib.auth.models import User
class newlist(models.Model):
user = models.ForeignKey(User)
list_name = models.CharField(max_length = 100)
picture = models.ImageField(upload_to='profiles/', default = "")
def __str__(self):
return self.list_name
class newlistitem(models.Model):
user = models.ForeignKey(User)
list_name = models.ForeignKey(newlist)
list_item = models.CharField(max_length = 200)
def __str__(self):
return self.list_item
HTML
{% for lista in lists %} <!-- This is a call to the lists that exist for eaach model taht s instantiates -->
<div id = "indivlistitem">
<b>{{lista}}</b><br> <!-- This is the title of each list that is rendered to the HTML -->
{% for eachlistitem in listitems %}
{{eachlistitem}}
{%endfor%}
</div>
{% endfor %}
据我了解,您的上下文中不需要列表项,拥有新列表就足够了。您也可以访问模板中的查询,因此您可以直接在模板中访问外键。像这样:
{% for eachlistitem in lista.newlistitem_set.all %}
。
这将为您提供当前列表为 list_name
的所有列表项
问题出在你models.py中的listitems
定义。具体来说,您将 lists
定义为与给定用户相关的所有列表,这很好,但随后将 listitems
定义为与该用户相关的每个列表 (filter(list_name=lists
) 相关的每个列表项,这很糟糕。
因为请求上下文只是 python 字典,并且可以嵌套,所以我会做如下的事情:
def control_panel(request, username):
context = RequestContext(request)
if username == request.user.username:
if request.user.is_authenticated():
user = User.objects.get(username=username)
lists = request.user.newlist_set.all()
listitems = {}
for list in lists:
listitems[list.list_name] = request.user.newlistitem_set.filter(list_name=list)
return render_to_response('controlpanel.html', {'lists': lists,'listitems':listitems,}, context)
else:
return render_to_response('login.html', {}, context)
else:
return render_to_response('controlpanel.html', {}, context)
这将创建一个字典,每个列表一个条目,然后您可以在模板中呈现:
{% for lista in lists %} <!-- This is a call to the lists that exist for eaach model taht s instantiates -->
<div id = "indivlistitem">
<b>{{lista}}</b><br> <!-- This is the title of each list that is rendered to the HTML -->
{% for eachlistitem in listitems.{{lista.list_name}} %}
{{eachlistitem}}
{%endfor%}
</div>
{% endfor %}
我试图只打印与我的模型的外键关联的条目。我有一系列列表,我想打印列表名称和该列表下的所有项目。我当前的问题是每个列表都有一个 div 生成,这很好,但是与每个列表关联的每个项目也在 div 内部打印出来,而不仅仅是与关联的项目列表名称。
观看次数
def control_panel(request, username):
context = RequestContext(request)
if username == request.user.username:
if request.user.is_authenticated():
user = User.objects.get(username=username)
lists = request.user.newlist_set.all()
listitems = request.user.newlistitem_set.all().filter(list_name = lists)
return render_to_response('controlpanel.html', {'lists': lists,'listitems':listitems,}, context)
else:
return render_to_response('login.html', {}, context)
else:
return render_to_response('controlpanel.html', {}, context)
型号
from django.db import models
from django.contrib.auth.models import User
class newlist(models.Model):
user = models.ForeignKey(User)
list_name = models.CharField(max_length = 100)
picture = models.ImageField(upload_to='profiles/', default = "")
def __str__(self):
return self.list_name
class newlistitem(models.Model):
user = models.ForeignKey(User)
list_name = models.ForeignKey(newlist)
list_item = models.CharField(max_length = 200)
def __str__(self):
return self.list_item
HTML
{% for lista in lists %} <!-- This is a call to the lists that exist for eaach model taht s instantiates -->
<div id = "indivlistitem">
<b>{{lista}}</b><br> <!-- This is the title of each list that is rendered to the HTML -->
{% for eachlistitem in listitems %}
{{eachlistitem}}
{%endfor%}
</div>
{% endfor %}
据我了解,您的上下文中不需要列表项,拥有新列表就足够了。您也可以访问模板中的查询,因此您可以直接在模板中访问外键。像这样:
{% for eachlistitem in lista.newlistitem_set.all %}
。 这将为您提供当前列表为 list_name
的所有列表项问题出在你models.py中的listitems
定义。具体来说,您将 lists
定义为与给定用户相关的所有列表,这很好,但随后将 listitems
定义为与该用户相关的每个列表 (filter(list_name=lists
) 相关的每个列表项,这很糟糕。
因为请求上下文只是 python 字典,并且可以嵌套,所以我会做如下的事情:
def control_panel(request, username):
context = RequestContext(request)
if username == request.user.username:
if request.user.is_authenticated():
user = User.objects.get(username=username)
lists = request.user.newlist_set.all()
listitems = {}
for list in lists:
listitems[list.list_name] = request.user.newlistitem_set.filter(list_name=list)
return render_to_response('controlpanel.html', {'lists': lists,'listitems':listitems,}, context)
else:
return render_to_response('login.html', {}, context)
else:
return render_to_response('controlpanel.html', {}, context)
这将创建一个字典,每个列表一个条目,然后您可以在模板中呈现:
{% for lista in lists %} <!-- This is a call to the lists that exist for eaach model taht s instantiates -->
<div id = "indivlistitem">
<b>{{lista}}</b><br> <!-- This is the title of each list that is rendered to the HTML -->
{% for eachlistitem in listitems.{{lista.list_name}} %}
{{eachlistitem}}
{%endfor%}
</div>
{% endfor %}