Django如何转发django-filter URL命名参数

问题描述 投票:0回答:1

我是django的新手,所以我可能会问一个愚蠢的问题,但是我应该如何将URL A的命名参数转发到URL B?

例如:

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

我真正想要的是:

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

我需要这样做,因为我需要将这些参数传递给在第二个URL处调用的视图,有人可以帮我吗?

EDIT1正在发挥作用的视图有以下两种:

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

以这种方式配置urls.py:

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

最后,我在html中拥有的按钮需要用查询字符串调用exportHostServices,以便我可以获取参数:

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 %}
python django django-filter
1个回答
0
投票

最后找出了代码的问题。问题是我传递查询字符串的形式默认为GET:

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

解决方法是将其简单地转换为POST形式,以便将查询字符串传递给视图:

<form action="{% url 'exportHostServices' %}?{{ request.GET.urlencode }}" method="post">
© www.soinside.com 2019 - 2024. All rights reserved.