英文:
Django get Models ID from Zip_longest() function data on HTML
问题
我正在开发一个Django应用程序,在其中我使用了Python的zip_longest
函数以及for
循环来在HTML表格中显示客户存款和取款记录,我想要将第二个zipped
列表项目的ID传递到按钮的URL中。我该如何实现这一目标。
这是我的模型(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}'
这是我的第一个视图(View)部分:
@login_required(login_url='user-login')
def account_statement(request, id):
try:
customer = Account.objects.get(id=id)
# 获取客户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]
# 获取客户取款记录通过ID,并按日期倒序显示最多5条记录
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.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 %}
这是我的URL路径部分:
urlpatterns = [
path('withdrawal/slip/<int:id>/', user_view.withdrawal_slip, name='withdrawal-slip'),
]
请理解我试图在withdrawal_slip
函数的URL路径中获取withdrawal id,但是我得到了这个错误:NoReverseMatch at /account/1/statement/ Reverse for 'withdrawal-slip' with arguments '()', not found. 1 pattern(s) tried: ['withdrawal/slip/(?P
英文:
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<id>[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.
答案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')
英文:
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论