django - 通过 url 传递过滤器变量并通过重定向保留它们
问题描述
我有两个问题:
我有一个返回到相同模板的重定向,应用了一个过滤器,它传递如下参数:
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 点相关的 url 中传递这个值。
我的代码
<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)
解决方案
您的 url 路径只接受 acontainer_id
所以您将无法将您想要的那些值作为 url 参数传递。您需要更改 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">
推荐阅读
- javascript - 修复不同大小的图块和精灵抖动的显示舍入错误
- php - 没有可用于包“pecl.php.net/mongodb”的版本
- kubernetes - Kubeflow 在本地设置(笔记本电脑/台式机)上缺少 .kube/config 文件
- sql - Postgresql:`pg_catalog.pg_stat_get_numscans` 需要 10 秒以上
- pytorch - 使用 s3 路径时如何避免将 tensorboard 日志写入本地目录?
- javascript - 带有 webpack + socket.io 的服务器/客户端应用程序
- apache - httpd.conf 和 logserver.conf 不能同时工作 - 它们使用相同的 PID (Linux)
- javascript - 如何在嵌套函数 React (navigator.geolocation.getCurrentPosition()) 中返回值
- r - 使用 VARCHAR 的 WHERE 语句抛出错误
- oauth-2.0 - 由于加密,AuthCode 授权不起作用