TypeError : got multiple values for argument 'reports_pk'

TypeError : got multiple values for argument 'reports_pk'

我目前 运行 在尝试将一些客户详细信息打印到 CSV 文件时遇到上述错误。

我明白错误的意思,但是,我看不出我的代码中哪里出现了错误。

请看下面的代码:

Views.py 函数:

def printViewCustomers(reports_pk):
    pkForm = get_object_or_404(SettingsClass, pk=reports_pk)

    complexName = pkForm.Complex

    connect = pyodbc.connect('DRIVER={SQL Server};'
                             'DATABASE=' + complexName + ';'
                             )

    viewCustomersSQL = ' Select Account , Name , Contact_Person , Telephone , Telephone2 , Fax1 , Fax2, EMail from dbo.Client Where DCLink <> 1 '

    cursor = connect.cursor();
    cursor.execute(viewCustomersSQL);
    viewCustomersData = cursor.fetchall()
    cursor.close()
    viewCustomers = []
    for row in viewCustomersData:
        rdict = {}
        rdict["Account"] = row[0]
        rdict["Name"] = row[1]
        rdict["Contact_Person"] = row[2]
        rdict["Telephone"] = row[3]
        rdict["Telephone2"] = row[4]
        rdict["Fax1"] = row[5]
        rdict["Fax2"] = row[6]
        rdict["Email"] = row[7]
        viewCustomers.append(rdict)

    # Starting CSV
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename=" ' + complexName + ' Customer Details.csv"'
    writer = csv.writer(response)

    writer.writerow([
        'Unit',
        'Name',
        'Contact Person',
        'Telephone 1',
        'Telephone 2',
        'E-mail'
    ])

    for x in viewCustomers:
        writer.writerow([
            x["Account"],
            x["Name"],
            x["Contact_Person"],
            x["Telephone"],
            x["Telephone2"],
            x["Email"]
        ])

HTML函数引用:

{% extends "main/base.html"%}

{% block content%}
<h1 style=" text-align: center">Reports</h1>
<hr>
 <br>

{% for x in model %}
    <div class="row mb-3">
        <div class="col">
            <a href="{% url 'DisplayCustomers' reports_pk=x.pk %}" class="list-group-item list-group-item-action list-group-item-primary">{{ x.Complex }} Details (Web View)</a>
        </div>
        <div class="col">
            <a href="{% url 'printViewCustomers' reports_pk=x.pk %}" class="list-group-item list-group-item-action list-group-item-success">Print {{ x.Complex }} Details to csv</a>
        </div>
    </div>
    <br>
{% endfor %}

{% endblock %}

URL 行

   path('accConnect/printViewCustomers/<int:reports_pk>', views.printViewCustomers , name='printViewCustomers'),

完整的错误信息:

TypeError at /accConnect/printViewCustomers/27
printViewCustomers() got multiple values for argument 'reports_pk'
Request Method: GET
Request URL:    http://127.0.0.1:8000/accConnect/printViewCustomers/27
Django Version: 3.2
Exception Type: TypeError
Exception Value:    
printViewCustomers() got multiple values for argument 'reports_pk'
Exception Location: C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py, line 181, in _get_response
Python Executable:  C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.4
Python Path:    
['C:\Users\KylePOG\Documents\GMA Programming\accConnect',
 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\python39.zip',
 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\DLLs',
 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\lib',
 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39',
 'C:\Users\KylePOG\AppData\Roaming\Python\Python39\site-packages',
 'C:\Users\KylePOG\AppData\Roaming\Python\Python39\site-packages\win32',
 'C:\Users\KylePOG\AppData\Roaming\Python\Python39\site-packages\win32\lib',
 'C:\Users\KylePOG\AppData\Roaming\Python\Python39\site-packages\Pythonwin',
 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\lib\site-packages']
Server time:    Thu, 13 Jan 2022 13:27:41 +0000
Traceback Switch to copy-and-paste view
C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py, line 47, in inner
                response = get_response(request) …
▶ Local vars
C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py, line 181, in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars

根据上面的代码,与我打印 CSV 的其他函数相比,一切似乎都是有序的

函数 printViewCustomers 的第一个参数必须是 request。只需将您的 views.py 更新为

def printViewCustomers(request, reports_pk):
    pkForm = get_object_or_404(SettingsClass, pk=reports_pk)

    complexName = pkForm.Complex

    ...........