Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delete all objects from model on specific value using button django

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 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 %}

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.

>Solution :

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" class="btn btn-outline-danger btn-sm">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" class="btn btn-outline-danger btn-sm">Delete Asset</button>
</form>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading