使用按钮 django 从特定值的模型中删除所有对象

Delete all objects from model on specific value using button django

有关上下文,请参阅 table 的屏幕截图:

我正在尝试使用相应的 删除资产 按钮删除 table 行 (即使用符号删除 django 模型中的所有行:1INCHUP)

我如何将其映射出来 删除资产 按钮会将相应的符号发送到以下视图:

View 仅供参考 - 此视图正在创建 table 但我正在尝试让删除按钮在这里工作

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

我假设这不需要表格,也不确定我是否走在正确的道路上,但任何建议都会很好。

您可能需要捕获 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>