如果没有对您的问题进行更深入的解释,就很难正确回答您的问题,但这里有:
一种选择:
# views.py
def mypage(request):
details = Personal_details.objects.select_related().filter(experience=3)
names = [d.reg.name for d in details]
return render(request, 'mypage.html', {'names': names})
# html
<p>People with 3 experience:</p>
<ul>
{% for name in names %}
<li>{{ name }}</li>
{% empty %}
<li>Nobody has 3 experience!</li>
{% endfor %}
</ul>
另外一个选项:
# views.py
def mypage(request):
details = Personal_details.objects.select_related().all()
names = [d.reg.name for d in details]
info = zip(details, names)
return render(request, 'mypage.html', {'info': info})
#html
<ul>
{% for detail, name in info %}
{% if detail.experience == 0 %}
<li>{{ name }} has 0 experience</li>
%{ elif detail.experience == 1 %}
<li>{{ name }} has 1 experience</li>
# and so on and so forth....
{% endif %}
{% endfor %}
</ul>
请注意我如何将您的模型(即类)名称大写。这是惯例,你应该遵守它。
所以这可能是也可能不是你希望做的,它可能不是最好的方法,但希望它能让你开始自己的解决方案。我建议阅读 Django视图和模板标签。