[Answered ]-Two (Almost) Identical View.py Functions Behaving Differently

1👍

Without seeing the rest of your code, I can’t answer the original question of why the two view functions behave differently.

As far as the issue with the anchor, you could just style the link to look like a button in bootstrap, like this:

<a href="{% url 'remove_item'%}?id={{item.id}}" class='btn btn-danger justify-content-right'>Remove</a>

Or, you could wrap the button in a form tag, like this (I have not tested this):

<form style="display: inline" action="{% url 'remove_item' %}" method="get">
  <button class='btn-danger justify-content-right'>Remove</button>
</form>

But GET requests are not secure and should never be used to modify a database. Instead you could use a POST request, like this:

<form action="{% url 'remove_item' %}" method="post">
    {% csrf_token %}
    <input type="hidden" name="item_id" value="{{ item.id }}">
    <button type='submit' class='btn-danger justify-content-right'>Remove</button>
</form>

This will send the item id through the hidden input, and then in your views you could access the same information in a more secure way than as part of a URL, which is what happens with a GET request.

def remove_item(request):
    if request.method == 'POST':
        item_id = request.POST.get('item_id')
        # Here you have your code that actually removes the item

0👍

I figured it out, guys. I missed up on the main app’s URL file. I had different views being called but they had the same path and the wrong one was listed first so it was being called before the one that was needed was. lol Sorry!

Leave a comment