我有2个问题:
我有一个返回到同一模板的重定向,有一个应用了过滤器的过滤器,该过滤器传递如下参数:http://localhost:99/depot/container/11/?area_easting=99&area_northing=&context_number=123&sample_number=&sample_type=organic
。当我重定向到同一模板时,参数丢失(如预期的那样),如何重定向和保留此参数?我也希望我需要一个“刷新查询”按钮。
在模板中,我可以传递操作和sample_id,但不能传递container_id。这来自m2m samples = models.ManyToManyField('Sample', through='ContainerSamples')
,我该如何在网址中传递此值-与第1点相关。
我的代码
<a href="{ url 'depot:change_container' operation='add' pk=sample.container.container_id fk=sample.sample_id }" class="badge badge-primary" role="button">
<<
</a>
# urls.py
path('container/<int:container_id>/', views.detailcontainer, name='detailcontainer'),
# views.py
def detailcontainer(request, container_id):
container = get_object_or_404(Container, pk=container_id)
samples = container.samples.all()
container_contents = container.samples.all()
unassigned_samples = Sample.objects.all()[:10]
unassigned_samples2 = Sample.objects.all()
qs = Sample.objects.all()
easting_query = request.GET.get('area_easting')
northing_query = request.GET.get('area_northing')
context_query = request.GET.get('context_number')
sample_number_query = request.GET.get('sample_number')
sample_type_query = request.GET.get('sample_type')
if easting_query != '' and easting_query is not None:
qs = qs.filter(area_easting__icontains=easting_query)
if northing_query != '' and northing_query is not None:
qs = qs.filter(area_northing__icontains=northing_query)
if context_query != '' and context_query is not None:
qs = qs.filter(context_number__icontains=context_query)
if sample_number_query != '' and sample_number_query is not None:
qs = qs.filter(sample_number__icontains=sample_number_query)
if sample_type_query != '' and sample_type_query is not None:
qs = qs.filter(sample_type__icontains=sample_type_query)
context = {
'queryset': qs,
'container':container,
'container_contents': container_contents,
'unassigned_samples': unassigned_samples,
}
return render(request, 'container/detailcontainer.html', context)
# template
{% for sample in queryset %}
<tr>
<td><a href="{ url 'depot:change_container' operation='add' pk=sample.container.container_id fk=sample.sample_id }" class="badge badge-primary" role="button">
<<
</a></td>
<td>{{ sample.sample_id }}</td>
<td>{{ sample.samples.container.container_id }}</td>
<td>{{ sample.area_easting }}.{{ sample.area_northing }}.{{ sample.context_number }}.{{ sample.sample_number }}</td>
<td>
{{ sample.sample_type }}
</td>
<td>{{ sample.taken_by }}</td>
</tr>
{% empty %}
<tr>
<td colspan="5">No data</td>
</tr>
{% endfor %}
编辑
该操作通过以下代码处理:
def change_container(request, operation, pk='', fk=''):
container = Container.objects.get(pk=pk)
sample = Sample.objects.get(pk=fk)
if operation == 'add':
ContainerSamples.add_to_container(container, sample)
elif operation == 'remove':
ContainerSamples.remove_from_container(container, sample)
# return redirect('depot:allcontainer')
return redirect('depot:detailcontainer', container_id=pk)
答案 0 :(得分:0)
您的网址路径仅接受container_id
,因此您将无法将所需的值作为网址参数传递。您要么需要更改URL路径,要么继续将它们作为查询字符串传递,我相信这就是您想要的。
要将它们作为查询字符串传递,您需要将在视图中收集的所有request.GET.get('qs')
包含到上下文中,并且它们将在模板上可用。
context = {
'queryset': qs,
'container':container,
'container_contents': container_contents,
'unassigned_samples': unassigned_samples,
'easting_query': easting_query,
'northing_query': northing_query,
'context_query': context_query,
'sample_number_query': sample_number_query,
'sample_type_query': sample_type_query
}
然后在模板上通过url标记传递它们。
<td><a href="{% url 'depot:change_container' container.container_id %}?operation=add&foo=bar&blabla=123" class="badge badge-primary" role="button">