首页 > 解决方案 > 通过 url 传递过滤器变量并通过重定向保留它们

问题描述

我有两个问题:

  1. 我有一个返回到相同模板的重定向,应用了一个过滤器,它传递如下参数:http://localhost:99/depot/container/11/?area_easting=99&area_northing=&context_number=123&sample_number=&sample_type=organic. 当我重定向到同一个模板时,参数会丢失(如预期的那样)我如何重定向并保留这些参数?我还希望我需要一个“刷新查询”按钮。

  2. 在模板中我可以传递操作和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) 

标签: django

解决方案


您的 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">


推荐阅读