Search code examples
pythondjangodjango-filter

Django how to forward django-filter URL named parameters


I'm new to django so i could be asking a dumb question, but how am i supposed to forward URL A's named parameter to URL B?

For example:

URL A = https://stackoverflow.com/questions?parameter=1
URL B = https://stackoverflow.com/ask

What i actually want is:

URL B = https://stackoverflow.com/ask?parameter=1

I need to do this because i need to pass those parameters to the view that is being called at the second url, could someone help me?

EDIT1 The views that are at play are this two:

class HostServiceListView(BaseFilterView, ListView):
    template_name = 'services/service_list.html'
    model = HostService
    paginate_by = 15
    context_object_name = 'services'
    filterset_class = HostServiceFilterSet

def exportHostServices(request):
    hsqs = HostService.objects.filter(hostname__icontains=request.GET.get("hostname_input", ''),\
                                        ip__icontains=request.GET.get("ip_input", ''),\
                                        port__icontains=request.GET.get("port_input", ''),\
                                        servicename__icontains=request.GET.get("servicename_input", ''))

    df = read_frame(hsqs)

    # my "Excel" file, which is an in-memory output file (buffer)
    # for the new workbook
    excel_file = IO()

    # pylint shows a false positive error here,
    # so the alert is suppressed with the comment after the code
    xlwriter = pd.ExcelWriter(excel_file, engine='xlsxwriter') # pylint: disable=abstract-class-instantiated

    df.to_excel(xlwriter, 'Host Services')

    xlwriter.save()
    xlwriter.close()

    # rewind the buffer
    excel_file.seek(0)

    # set the mime type so that the browser knows what to do with the file
    response = HttpResponse(excel_file.read(),\
        content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

    # set the file name in the Content-Disposition header
    response['Content-Disposition'] = 'attachment; filename=Anagrafica-Servizi.xlsx'

    return response

The urls.py is configured this way:

urlpatterns = [
    path('services/', HostServiceListView.as_view(), name='services'),
    path('services/exportHostServices/', exportHostServices, name='exportHostServices'),
    path('', IndexFilterView, name="index")
]

Finally, the button i've got in html that needs to call exportHostServices with the query-string so i can get the parameters:

services/service_list.html

{% extends 'services/base.html' %}

{% load paginatedfilter %}

{% block title %}Host Services{% endblock title %}

{% block content %}

    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">Service Registry</h1>
        <div class="btn-toolbar mb-2 mb-md-0">
            <div class="btn-group mr-2">
                <form action="{% url 'exportHostServices' %}?{{ request.GET.urlencode }}">
                    <input type="submit" class="btn btn-sm btn-outline-secondary" value="Export">
                </form>
            </div>
        </div>
    </div>

    <div class="table-responsive table-sm">

    <form method="GET">

        <input type="submit" class="btn-hidden"/>

        <table class="table table-hover table-light table-striped">
            <thead class="thead-light">
                <tr>
                    <th>{{ filter.form.hostname_input }}</th>
                    <th>{{ filter.form.ip_input }}</th>
                    <th>{{ filter.form.port_input }}</th>
                    <th>{{ filter.form.servicename_input }}</th>
                </tr>
            </thead>
            <tbody>
                {% for service in  services %}
                <tr>
                    <td>{{ service.hostname }}</td>
                    <td>{{ service.ip }}</td>
                    <td>{{ service.port }}</td>
                    <td>{{ service.servicename }}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>

        {% if is_paginated %}
            <ul class="pagination">
                {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?{% param_replace page=page_obj.previous_page_number %}" aria-label="previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                {% endif %}
                {% for i in paginator.page_range %}
                    {% if page_obj.number == i %}
                        <li class="page-item active" aria-current="page">
                            <span class="page-link">
                                {{ i }}
                                <span class="sr-only">(current)</span>
                            </span>
                        </li>
                    {% else %}
                        <li class="page-item">
                            <a class="page-link" href="?{% param_replace page=i %}">{{ i }}</a>
                        </li>
                    {% endif %}
                {% endfor %}
                {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?{% param_replace page=page_obj.next_page_number %}" aria-label="next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                {% endif %}
            </ul>
        {% endif %}
    </form>
</div>

{% endblock content %}

Solution

  • Figured out atlast what was wrong with the code. The problem was that the form where i passed the querystring was defaulted at GET:

    <form action="{% url 'exportHostServices' %}?{{ request.GET.urlencode }}">
    

    The fix was to simply convert it to a POST form so the querystring could be passed to the view:

    <form action="{% url 'exportHostServices' %}?{{ request.GET.urlencode }}" method="post">