模型实例设置 ||反转 '' 与参数 '('',)' 未找到。尝试了 1 种模式

Model Instance Settings || Reverse for '' with arguments '('',)' not found. 1 pattern(s) tried

我目前有一个旨在根据用户设置生成文档的应用程序。这些设置应该由程序根据 class 的实例设置读取,以便程序知道它正在为哪个设置生成文档。

在为用户创建实例设置以编辑每个模型条目时,代码是这样工作的:

    setting = get_object_or_404(SettingsClass, pk=setting_pk)
    if request.method == 'GET':
        form = SettingUpdateForm(instance=setting)
        return render(request, 'main/viewSettings.html', {'setting': setting, 'form':form})
    else:
        form =  SettingUpdateForm(request.POST, instance=setting)
        if form.is_valid():
            form.save()
            return redirect('settingsHome')

所以它使用“get_object_or_404”函数来 return 用户应该在其中工作的实例,并告诉表单使用那个 实例 这里:form = SettingUpdateForm(instance=setting)

但是,当我在从模型中读取时尝试执行此操作时,相同类型的设置不起作用。我目前的观点是这样设置的:

def printReports(request , reports_pk):
    pkForm = get_object_or_404(SettingsClass , pk=reports_pk)
    form= SettingsClass(instance=pkForm)

    complexName = form.Complex

我基本上是想告诉程序在模型的某个实例中工作并从那里读取项目,例如:complexName = form.Complex

如果有人有任何解决方案或替代方法来设置类似这样的东西,请提供帮助。我将在下面添加我的网址、模板和视图代码以便更好地查看

Views.py:

def reportsHome(request):
    model = SettingsClass.objects.all().order_by('Complex')

    content ={'model':model }
    return render(request, 'main/reportsHome.html' , content)

def printReports(request , reports_pk):
    pkForm = get_object_or_404(SettingsClass , pk=reports_pk)
    form= SettingsClass(instance=pkForm)

    complexName = form.Complex

    #CHECKING TRIAL BALANCE SETTINGS
    if form.Trial_balance_Year_to_date == True:
        printTrialBalanceYTD = True

        ### Printing Trial Balance PDF
        response = HttpResponse(content_type= 'application/pdf')
        response['Content-Disposition']= 'attachment; filename=TrialBalance' + \
            str(datetime.now()) + '.pdf'
        response['Content-Transfer-Encoding'] = 'binary'

        #SQL STATEMENT
        baseSelect = 'SELECT '+ op5 + op4 + ' Debit , Credit FROM [?].[dbo].[PostGL] AS genLedger '
        xtrbYTD = baseSelect + trbYTD + op1 + op2 + op3 + op6

        cursor = cnxn.cursor();
        cursor.execute(baseTRBYear, [complexName], [complexName], [complexName], [one_yrs_ago]);
        xAll = cursor.fetchall()
        cursor.close()
        xtrbYTD = []
        for row in xtrbYTD:
            rdict = {}
            rdict["Description"] = row[0]
            rdict["Account"] = row[1]
            rdict["Debit"] = row[2]
            rdict["Credit"] = row[3]
            arr_trbYTD.append(rdict)

        content =  {"arr_trbYTD":arr_trbYTD , 'xCreditTotal':xCreditTotal , 'xDebitTotal':xDebitTotal , 'complexName':complexName , 'openingBalances': openingBalances ,'printZero':printZero}
        html_string=render_to_string('main/pdf-trialbalance.html' , content)
        html=HTML(string=html_string)

        result=html.write_pdf()

        with tempfile.NamedTemporaryFile(delete=True) as output:
            output.write(result)
            output.flush()

            output.seek(0)
            response.write(output.read())

            return response

    else:
        printTrialBalanceYTD = False

urls.py:

#Reports
path('accConnect' , views.reportsHome, name='reportsHome'),
path('accConnect/printReports/<int:reports_pk>' , views.printReports , name='printReports')

模板:

reportsHome.html:

{% block content%}
<h1 style=" text-align: center">Reports</h1>
<hr>
 <br>
 <div class="list-group">
     <a href="#" class='list-group-item active'>Print Single Complex's</a>
{% for x in model %}
    <a href="{% url 'printReports'  %}" class="list-group-item list-group-item-action" >{{ x.Complex }} Reports</a>
{% endfor %}
</div>
{% endblock %}

pdf-trialbalance.html:

{% block content%}
<h1 class = 'center'>Kyle Database Trial Balance</h1>
<br>

</div>
<br>
<br>

<div class="table-container">
  <table style="width: 100%">
    <th >Account</th>
    <th>Description</th>
    <th>Debit</th>
    <th>Credit</th>
    {% for arr_trbYTD in arr_trbYTD %}
      <tr>
        <td>{{ arr_trbYTD.Description }}</td>
        <td>{{ arr_trbYTD.Account }}</td>
        <td>
        {%if arr_trbYTD.Debit > 0%}
            {{arr_trbYTD.Debit}}
        {%endif%}
        </td>
        <td>
        {%if arr_trbYTD.Credit > 0%}
          {{arr_trbYTD.Credit}}
        {%endif%}
        </td>
      </tr>
    <tr >
    {% endfor %}
    <td> <b>Totals</b> </td>
    <td> </td>
    {% for xDebitTotal in xDebitTotal %}
    <td><b>R {{ xDebitTotal }}</b></td>
    {% endfor %}

    {% for xCreditTotal in xCreditTotal %}
    <td><b>R {{ xCreditTotal }}</b></td>
    {% endfor %}

    </tr>
  </table>
</div>
<br>
<br>
<br>
{% endblock %}

您没有将“reports”变量传递给上下文。这就是 PK 值为空的原因。检查正在呈现的视图 reportsHome.html 并将必要的变量应用于模板,例如:

def view_func(request)
......
return render(request, '<template_name>.html', {"report": report }) 

要解决标题中的错误,首先您需要修复模板中url的用法,并将SettingsClass实例的主键传递给:

{% url 'printReports' x.pk %}

所以:

{% for x in model %}
    <a href="{% url 'printReports' x.pk %}" class="list-group-item list-group-item-action" >{{ x.Complex }} Reports</a>
{% endfor %}

然后在您看来,您不需要使用表单来访问 SettingsClass 实例中的属性,因此您只需执行以下操作:

def printReports(request , reports_pk):
    settings_instance = get_object_or_404(SettingsClass , pk=reports_pk)

    complexName = settings_instance.Complex

    #CHECKING TRIAL BALANCE SETTINGS
    if settings_instance.Trial_balance_Year_to_date == True:
        ...