0

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 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.

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks heaps! That is working great. Would you mind clarifying the difference between if symbol and request.METHOD == "POST": and if request.METHOD == "POST": ? They both seem to be working.
If you accidentally send a POST request to my-dashboard/, the value of symbol will be None and you may delete all the CryptoAssets whith symbol=None. Maybe you don't have any data with symbol=None and isn't dangerous in your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.