I'm trying to pass a queryset and a dictionary, context is the queryset, and for this example unassigned_samples2 the dictionary, in my template I can either get the dictionary to display or the queryset, but not both, it depends if I include the context queryset. Any ideas how to get this working?

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)

    qs = qs

    context = {
        'queryset': qs
    }
    return render(request, 'container/detailcontainer.html', context,
    {'container':container,
    'container_contents': container_contents,
    'unassigned_samples': unassigned_samples,
    })

2 Answers

1
hygull On Best Solutions

Based on your django version, you can check https://docs.djangoproject.com/en/2.2/topics/http/shortcuts/ (select specific version from bottom right of the page) for the signature of render() function.

The signature of render() function is render(request, template_name, context=None, content_type=None, status=None, using=None).

You can see, 3rd parameter is context (a dictionary containing keys which are used as temaplate context variables in django template).

Just change

context = {
    'queryset': qs
}

return render(request, 'container/detailcontainer.html', context,
{'container':container,
'container_contents': container_contents,
'unassigned_samples': unassigned_samples,
})

to

context = {
    'queryset': qs
    'container': container,
    'container_contents': container_contents,
    'unassigned_samples': unassigned_samples
}

return render(request, 'container/detailcontainer.html', context)

Finally, in your template you can access all the defined template context variables like queryset, container etc.

e.g.

  • {% for sample in queryset %} {{sample.pk}} {% endfor %}

  • {% for item in unassigned_samples %} {{item.pk}} {% endfor %}

0
Atcrank On

Wouldn't you add: {'container':container, 'container_contents': container_contents, 'unassigned_samples': unassigned_samples, }

To your context dictionary? The context dictionary can have lots of objects in it.