Django get Models ID from Zip_longest() function data on HTML

Question:

I am working a Django application where I have used python zip_longest function with a the for loop for displaying both the Customer Deposits and Withdrawals in an HTML Table, and I want to get the second zipped list item ID unto a url in a button. How do I achieve this.
Here is my Model:

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}'

Here is my first view:

@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)

Here is my HTML code:

{% 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 %}

Here is my URL path code:

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

Please, understand I am trying to get the withdrawal id for the withdrawal_slip function URL Path but this is the error I am getting NoReverseMatch at /account/1/statement/
Reverse for ‘withdrawal-slip’ with arguments ‘(”,)’ not found. 1 pattern(s) tried: [‘witdrawal/slip/(?P[0-9]+)/Z’]
. The error is pointing to the Button’s URL code number in my HTML but I don’t know what is the issue.

Asked By: apollos

||

Answers:

It should be withdrawal.id instead of withdrawal.withdrawal_id, like so:

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

Or maybe you want transID if you send transID as:

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

So remeber to change <int:id> to <str:trans_id> since it is CharField, like so:

path('withdrawal/slip/<str:trans_id>/', user_view.withdrawal_slip, name='withdrawal-slip')
Answered By: Sunderam Dubey