采用以下模型:
class InsName(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=30, verbose_name = "Insurer/Broker")
def __str__(self):
return self.name
class Development(models.Model):
id = models.AutoField(primary_key=True)
logno = models.CharField(max_length=13, unique=True)
insurer = models.ForeignKey(InsName, on_delete=models.SET_NULL, null=True, blank=True)
Phase_status = (
('dev','Development'),
('review','Review')
)
phase = models.CharField(max_length=10,
choices = Phase_status,
default='review',
)
user = models.ForeignKey(User, on_delete=models.CASCADE)
date_added = models.DateField(default=datetime.date.today)
comments = models.TextField(max_length=350)
def get_absolute_url(self):
return reverse('development-detail', args=[str(self.id)])
def __str__(self):
"""String for representing the Model object."""
return f'{self.id} ({self.insurer})'
我有一个模板,可以输出“发展”表。我想要的是单击Insurer
时会弹出一个模板,其中包含针对该选择过滤的所有开发。
我一直在尝试各种方法,但是我不知道如何将变量Insurer
的名称导入视图:
def insurer_filter(request):
insname = InsName.objects.filter(name = {{ value }})
ins_name = Development.objects.filter(insurer = insname)
return render(request, 'insurer_filter.html', context = ins_name)
请原谅我的躲猫猫编码(否则任何建议都是有帮助的!)。我对此完全陌生,只是发现自己的脚而已,但是在上述操作上我找不到任何帮助。
当前开发列表模板:
{% extends "base_generic.html" %}
{% block content %}
<h1>My Developments</h1>
{% if mydevs %}
<table style="width:100%">
<thead>
<tr>
<td>Log Number</td>
<td>Client</td>
</tr>
</thead>
<tbody>
{% for development in mydevs %}
<tr>
<td><a href="{{ development.get_absolute_url }}">{{development.logno}}</a></td>
<td><a href="insurer_filter/?name={{ value }}">{{development.insurer}}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
然后我想我的新insurer_filter模板将类似于:
{% extends "base_generic.html" %}
{% block content %}
<h1><Value> Developments</h1>
{% if ???%}
<table style="width:100%">
<thead>
<tr>
<td>Log Number</td>
<td>Client</td>
</tr>
</thead>
<tbody>
{% for development in wilsons %}
<tr>
<td><a href="{{ development.get_absolute_url }}">{{development.logno}}</a></td>
<td>{{development.insurer}}</td>
</tr>
{% endfor %}
</tbody>
</table>
答案 0 :(得分:1)
尝试一下:
urls.py:
//updated element
pieElem.innerHTML =
'<div id="myDiv" style="position:absolute;text-align:center;font-size:16px;">' +
'Total' + total +
'</div>';
views.py:
url(r'insurer_filter/(?P<name>[A-Za-z]+)/$', views.insurer_filter),
答案 1 :(得分:0)
据我了解,您需要有一个基本视图,该视图可返回所有发展情况的列表,并可以按保险人筛选发展情况。
您可以从您的观点开始:
def developments_list(request):
if request.GET.get('insurer_id'):
# Check if we need to filter developments by insurer
developments = Development.objects.filter(insurer__pk=request.GET.get('insurer_id'))
else:
# otherwise return all developments records
developments = Development.objects.all()
return render(request, 'developments_list.html', context={'developments': developments})
在您的urls.py
中from my_views import developments_list
path('developments/', developments_list, name='developments')
在模板中,您可以为每个“开发”记录提供链接:
{% for d in developments %}
<a href="{% url 'my_views:developments' %}?insurer_id={{ d.insurer.pk }}">{{ d.id }} {{ d.insurer.name }}</a> |
{% endfor %}
正如您在上面看到的,我们正在传递保险人ID,以便按保险人过滤记录。
您还可以放置一个链接以重置过滤器:
<a href="{% url 'my_views:developments' %}">Reset filter</a>
这是解决问题的非常简单的方法,您也可以使用基于类的视图,以使视图看起来更优雅。