【问题标题】:Django how to forward django-filter URL named parametersDjango如何转发django-filter URL命名参数
【发布时间】:2020-03-19 15:05:54
【问题描述】:

我是 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 %}

【问题讨论】:

  • 这能回答你的问题吗? Django return redirect() with parameters
  • @IvanStarostin 感谢您的回答:-) 我认为这可能有用,但不明白我应该如何实现它。比方说,我已经有 view1 生成并使用 html 查询字符串和 view2,它指向 urls.py 中的另一个 url,需要该查询字符串来过滤模型数据。我应该如何实现它?
  • 请出示您的代码。
  • 代码已添加,感谢@IvanStarostin
  • 哪个视图呈现这个模板?

标签: python django django-filter


【解决方案1】:

最后弄清楚代码有什么问题。问题是我传递查询字符串的表单默认为 GET:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 2019-08-22
    • 2015-02-07
    • 2016-01-05
    • 1970-01-01
    • 2022-01-24
    • 2013-08-31
    相关资源
    最近更新 更多