Django 从 HTML 上的 Zip_longest() 函数数据中获取模型 ID

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

我正在开发一个 Django 应用程序,我在其中使用带有 for 循环的 python zip_longest 函数来在 HTML 表中显示客户存款和取款,我想将第二个压缩列表项 ID 获取到按钮中的 url。我如何实现这一目标。 这是我的模型:

class Witdrawal(models.Model): 
    account = models.ForeignKey(Profile, on_delete=models.CASCADE, null=True)
    transID = models.CharField(max_length=12, null=True)
    staff = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    withdrawal_amount = models.PositiveIntegerField(null=True)
    date = models.DateTimeField(auto_now_add=True)


    def __str__(self):
        return f'{self.account}- Withdrawn - {self.withdrawal_amount}'

这是我的第一个观点:

@login_required(login_url='user-login')

def account_statement(request, id):

    try:
       customer = Account.objects.get(id=id)
       #Get Customer ID
       customerID = customer.customer.id
        
    except Account.DoesNotExist:
        messages.error(request, 'Something Went Wrong')
        return redirect('create-customer')
    else:
        deposits = Deposit.objects.filter(customer__id=customerID).order_by('-date')[:5]
        #Get Customer Withdrawal by ID and order by Date minimum 5 records displayed
        withdrawals = Witdrawal.objects.filter(account__id=customerID).order_by('-date')[:5]
   context = {
    "deposits_and_withdrawals": zip_longest(deposits, withdrawals, fillvalue='No Transaction'),
   }
   return render(request, 'dashboard/statement.html', context)

这是我的 HTML 代码:

{% if deposits_and_withdrawals %}
                    <tbody>
                    
                      
                      {% for deposit, withdrawal in deposits_and_withdrawals %}  
                      <tr>
                        <td style="background-color:rgba(231, 232, 233, 0.919); color:blue;">Deposit - </td>
                        <td style="background-color:rgba(231, 232, 233, 0.919)">{{ deposit.acct }}</td>
                        <td style="background-color:rgba(231, 232, 233, 0.919)">{{ deposit.transID }}</td> 
                        <td style="background-color:rgba(231, 232, 233, 0.919)">N{{ deposit.deposit_amount | intcomma }}</td>
                        <td style="background-color:rgba(231, 232, 233, 0.919)">{{ deposit.date | naturaltime }}</td>
                        
                        <th scope="row" style="background-color:rgba(231, 232, 233, 0.919)"><a class="btn btn-success btn-sm" href="{% url 'deposit-slip' deposit.id %}">Slip</a></th>
                      </tr>

                      <tr style="color: red;">
                        <td>Withdrawal - </td>
                        <td>{{ withdrawal.account.surname }}</td>
                        <td>{{ withdrawal.transID }}</td> 
                        <td>N{{ withdrawal.withdrawal_amount | intcomma }}</td>
                        <td>{{ withdrawal.date | naturaltime }}</td>
                        
                        <th scope="row"><a class="btn btn-success btn-sm" href=" {% url 'withdrawal-slip' withdrawal.withdrawal_id %} ">Slip</a></th>
                      </tr>
                      {% endfor %}  
                                 
                    </tbody>
                    {% else %}
                    <h3 style="text-align: center; color:red;">No Deposit/Withdrawal Found for {{ customer.customer.profile.surname }} {{ customer.customer.profile.othernames }}</h3>
                    {% endif %}

这是我的网址路径代码:

urlpatterns = [
path('witdrawal/slip/<int:id>/', user_view.withdrawal_slip, name = 'withdrawal-slip'),
]

请理解,我正在尝试为 withdrawal_slip 函数 URL 路径获取 withdrawal id 但这是我在 /account/1/statement/ 得到 NoReverseMatch 的错误 与未找到参数“(”,)“的”取款单“相反。尝试了 1 种模式:['wittrawal/slip/(?P[0-9]+)/\Z']。错误指向我的 HTML 中按钮的 URL 代码编号,但我不知道是什么问题。

python django django-views django-templates django-urls
1个回答
0
投票

应该是

withdrawal.id
而不是
withdrawal.withdrawal_id
,像这样:

{% url 'withdrawal-slip' withdrawal.id %}

或者如果您将

transID
发送为:
,也许您想要
transID

{% url 'withdrawal-slip' withdrawal.transID %}

所以记得把

<int:id>
改成
<str:trans_id>
因为它是
CharField
,像这样:

path('withdrawal/slip/<str:trans_id>/', user_view.withdrawal_slip, name='withdrawal-slip')
© www.soinside.com 2019 - 2024. All rights reserved.