Home > front end >  Delete all objects from model on specific value using button django
Delete all objects from model on specific value using button django

Time:03-16

Please see screen shot of table for context: enter image description here

I'm trying to delete the table row using the corresponding Delete Asset button (i.e. delete all rows in a django model with Symbol: 1INCHUP)

How I am mapping this out would be the Delete Asset button would send the corresponding Symbol to the following view:

View FYI - this View is creating the table but I'm trying to get the delete button working here

# 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 >${{ row.total_cost }}</span></td>
  <td style="text-align:center"><span >${{ row.purchase_price }}</span></td>
  <td style="text-align:center"><span >${{ row.current_price }}</span></td>
  <td style="text-align:center"><span >${{ row.profit_loss }}</span></td>
  <td style="text-align:center"><span >{{ 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 %}" >Delete Asset</button>
     </form>
  </td>
</tr>
{% endfor %}

URLS

path('my-dashboard/', get_asset_price, name='dashboard'),

Error

TypeError: get_asset_price() missing 1 required positional argument: 'symbol'

I'm assuming a form isn't needed for this and also unsure if I'm on the correct path but any advice will be great.

CodePudding user response:

You may need catch the optional symbol parameter in the url and send it to the view.

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'),

And send the form to the correct url to delete the data.

<form action="{% url 'dashboard-delete' row.symbol %}" method="POST">
    {% csrf_token %}
    <button type="submit" >Delete Asset</button>
</form>

Aditionaly you can add a confirm message before send the delete request.

<form onsubmit="return confirm('Are you sure?');" action="{% url 'dashboard-delete' row.symbol %}" method="POST">
    {% csrf_token %}
    <button type="submit" >Delete Asset</button>
</form>
  • Related