我的模板中有一个钱包地址列表。当我尝试通过单击删除按钮删除地址时,它返回了一个错误消息。我认为这是我的模板结构中的一个错误。但是,我真的不知道。当我尝试通过在删除视图URL中手动输入地址的ID来删除地址时,它被删除了。下面是返回的错误消息。
Traceback (most recent call last):
File "C:\Users\admin\Documents\Django\env\lib\site-packages\django\db\models\fields\__init__.py", line 2018, in get_prep_value
return int(value)
The above exception (invalid literal for int() with base 10: 'Btc') was the direct cause of the following exception:
File "C:\Users\admin\Documents\Django\env\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\admin\Documents\Django\env\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\admin\Documents\Django\env\lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\admin\Documents\Django\crypto\dashboard\views.py", line 275, in del_wallet
wallet.delete()
File "C:\Users\admin\Documents\Django\env\lib\site-packages\django\db\models\base.py", line 1137, in delete
return collector.delete()
File "C:\Users\admin\Documents\Django\env\lib\site-packages\django\db\models\deletion.py", line 475, in delete
query.update_batch(
File "C:\Users\admin\Documents\Django\env\lib\site-packages\django\db\models\sql\subqueries.py", line 78, in update_batch
self.get_compiler(using).execute_sql(NO_RESULTS)
File "C:\Users\admin\Documents\Django\env\lib\site-packages\django\db\models\sql\compiler.py", line 1819, in execute_sql
cursor = super().execute_sql(result_type)
File "C:\Users\admin\Documents\Django\env\lib\site-packages\django\db\models\sql\compiler.py", line 1382, in execute_sql
sql, params = self.as_sql()
File "C:\Users\admin\Documents\Django\env\lib\site-packages\django\db\models\sql\compiler.py", line 1785, in as_sql
val = field.get_db_prep_save(val, connection=self.connection)
File "C:\Users\admin\Documents\Django\env\lib\site-packages\django\db\models\fields\related.py", line 1146, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "C:\Users\admin\Documents\Django\env\lib\site-packages\django\db\models\fields\__init__.py", line 925, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\admin\Documents\Django\env\lib\site-packages\django\db\models\fields\__init__.py", line 2703, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\admin\Documents\Django\env\lib\site-packages\django\db\models\fields\__init__.py", line 2020, in get_prep_value
raise e.__class__(
Exception Type: ValueError at /dashboard/wallet/2/delete/
Exception Value: Field 'id' expected a number but got 'Btc'.
views.py
@login_required
def wallet(request):
user = request.user
wallet=Wallet(user=user)
object_list = Wallet.objects.filter(user=user)
# Paginator
paginator = Paginator(object_list, 2)
page = request.GET.get('page')
try:
ref = paginator.page(page)
except PageNotAnInteger:
ref = paginator.page(1)
except EmptyPage:
ref = paginator.page(paginator.num_pages)
if request.method == 'POST':
if 'add' in request.POST:
form = WalletForm(request.POST, instance=wallet)
if form.is_valid():
form.save()
messages.success(request, ("New wallet address successfully added."))
return redirect("dashboard:wallet")
else:
form = WalletForm(instance=user)
context = {
'form': form,
'ref': ref,
# 'refe': refe
# 'del_wallet': del_wallet
}
return render(request, 'dashboard/wallet.html', context)
@login_required
def del_wallet(request, pk):
wallet = get_object_or_404(Wallet, id=pk)
wallet.delete()
return redirect("dashboard:wallet")
第一个视图wallet
负责让用户添加新的wallet,同时也负责显示wallet。添加wallet逻辑是通过使用弹出模态来完成的。第二个视图del_wallet
负责处理删除逻辑。
urls.py
urlpatterns = [
path('wallet', wallet, name="wallet"),
path('wallet/<str:pk>/delete/', del_wallet, name="del_wallet"),
]
通过输入URL并指定id,带有id的wallet地址将被删除。例如,调用 http://localhost“5000/wallet/2/delete/ 将删除id为2的地址。但是单击每个wallet附带的delete按钮,将返回上面指定的错误消息。我添加了print语句来检查每次单击delete按钮时弹出的wallet的详细信息。它记录了单击的带有id的正确wallet,但仍返回错误消息。我似乎不知道是什么导致了错误。
wallet.html
<h3 class="h5 text-primary pt-1 pt-lg-3 mt-4">Available Wallets</h3>
<div class="table-responsive">
<table class="table table-dark table-striped">
<thead>
<tr>
<th>SN</th>
<th>Type</th>
<th>Address</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<!-- -->
{% for i in ref %}
<tr>
<td style="line-height: 1.3; padding-top: 1.6em;">{{ forloop.counter }}</td>
<td style="padding-top: 20px;">{{ i.get_type_display }}</td>
<td style="padding-top: 20px;">{{ i.address }}</td>
<td class="view" data-bs-toggle="modal" data-bs-target="#wallet{{ i.pk }}">
<a style="text-decoration: none;" href="#wallet{{ i.pk }}">
Delete
</a>
</td>
</tr>
<!-- DELETE MODAL -->
<div class="modal fade" id="wallet{{ i.pk }}" tabindex="-1" role="dialog" style="margin-top: 10em;">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content" style="background: #141822;">
<div class="modal-body">
<h6>Confirm Delete</h6>
<p>Are you sure you want to delete
<span style="color: rgb(10, 223, 165);">
{{ i.type }} - {{ i.address }}
</span> wallet?
</p>
<button type="submit" class="btn btn-primary btn-sm w-100">
<a href="/dashboard/wallet/{{ i.pk }}/delete/" style="text-decoration: none;">
Confirm
</a>
</button><br>
<button type="button" class="btn btn-warning btn-sm w-100 mt-2" data-bs-dismiss="modal">
Close
</button>
</div>
</div>
</div>
</div>
{% empty %}
<tr>
<td>You haven't added a wallet address yet.</td>
<td></td>
<td></td>
<td></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
models.py
class Wallet(models.Model):
wallet = (
('Btc', 'Bitcoin'),
('Eth', 'Ethereum'),
('Trc20', 'Tether-UDST'),
('Ltc', 'Litecoin')
)
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
type = models.CharField(choices=wallet, default='Btc')
address = models.CharField(max_length=80)
请问,什么是可能的错误的原因。我已经阅读了许多帖子在线,但似乎没有帮助解决我的问题。
2条答案
按热度按时间2uluyalo1#
首先它应该是
<int:pk>
而不是<str:pk>
,其次在锚标记中使用url标记,因此:最好将URL更改为:
**注意:**始终在每条路线的末尾添加
/
。jrcvhitl2#
只接受这样的链接
删除此