請參閱表格的螢屏截圖以了解背景關系:

我正在嘗試使用相應的Delete Asset按鈕洗掉表行(即使用 Symbol: 1INCHUP洗掉 django 模型中的所有行)
我如何將其映射出來是洗掉資產按鈕會將相應的符號發送到以下視圖:
查看 僅供參考 - 此視圖正在創建表格,但我正在嘗試讓洗掉按鈕在這里作業
# CryptoAssets is a model
def get_asset_price(request, symbol):
sym = CryptoAssets.objects.filter(user_id=request.user.id).values('symbol')
obj = sym.annotate(total_units=Sum('units'),total_cost=Sum('cost')).order_by('symbol')
cp = [CryptoPrices.objects.filter(ticker=s['symbol']).values('current_price').order_by('ticker')[0] for s in sym]
for i, o in enumerate(obj):
o.update({'purchase_price': round(o['total_cost']/o['total_units'], 5)})
o.update({'current_price': cp[i]['current_price']})
pl = cp[i]['current_price']*o['total_units']-o['total_cost']
o.update({'profit_loss': round(pl, 5)})
o.update({'profit_loss_p': round(pl/o["total_cost"]*100, 2)})
# Delete Asset request
if request.METHOD == "POST":
asset = CryptoAssets.objects.filter(user_id=request.user.id, symbol=symbol)
asset.delete()
return redirect('coinprices/my-dashboard.html')
context = {
'object': obj,
}
return render(request, 'coinprices/my-dashboard.html', context)
HTML
{% for row in object %}
<tr>
<td style="text-align:center">{{ row.symbol }}</td>
<td style="text-align:center">{{ row.total_units }}</td>
<td style="text-align:center"><span class="prefix">${{ row.total_cost }}</span></td>
<td style="text-align:center"><span class="prefix">${{ row.purchase_price }}</span></td>
<td style="text-align:center"><span class="prefix">${{ row.current_price }}</span></td>
<td style="text-align:center"><span class="prefix">${{ row.profit_loss }}</span></td>
<td style="text-align:center"><span class="suffix">{{ row.profit_loss_p }}%</span></td>
<td style="background-color:white; border: 1px solid white;">
<form action="." method="POST">{% csrf_token %}
<button href="{% url 'dashboard' row.symbol %}" class="btn btn-outline-danger btn-sm">Delete Asset</button>
</form>
</td>
</tr>
{% endfor %}
網址
path('my-dashboard/', get_asset_price, name='dashboard'),
錯誤
TypeError: get_asset_price() missing 1 required positional argument: 'symbol'
我假設這不需要表格,也不確定我是否走在正確的道路上,但任何建議都會很棒。
uj5u.com熱心網友回復:
您可能需要在 url 中捕獲可選 symbol引數并將其發送到視圖。
def get_asset_price(request, symbol=None):
# ...
if symbol and request.METHOD == "POST":
# delete ...
return redirect('dashboard')
path('my-dashboard/', get_asset_price, name='dashboard'),
re_path('my-dashboard/(?P<symbol>\w )', get_asset_price, name='dashboard-delete'),
并將表單發送到正確的 url 以洗掉資料。
<form action="{% url 'dashboard-delete' row.symbol %}" method="POST">
{% csrf_token %}
<button type="submit" class="btn btn-outline-danger btn-sm">Delete Asset</button>
</form>
此外,您可以在發送洗掉請求之前添加確認訊息。
<form onsubmit="return confirm('Are you sure?');" action="{% url 'dashboard-delete' row.symbol %}" method="POST">
{% csrf_token %}
<button type="submit" class="btn btn-outline-danger btn-sm">Delete Asset</button>
</form>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444826.html
