Python version 3.7.7 and Django version 2.2.3. Code on Github https://github.com/jcwill415/Stock_Market_Web_App
I want to add a delete button in the last column of a table. When the code is outside of the table, it works to delete an entry from the table. But when the code is inside the table, I receive a NoReverseMatch error that says:
NoReverseMatch at /add_stock.html
Reverse for 'delete' with arguments '('',)' not found. 1 pattern(s) tried: ['delete/(?P<stock_id>[^/]+)$']
Request Method: GET
Request URL: http://localhost:8000/add_stock.html
Django Version: 2.2.3
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'delete' with arguments '('',)' not found. 1 pattern(s) tried: ['delete/(?P<stock_id>[^/]+)$']
Exception Location: C:\djangostock\venv\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 668
Python Executable: C:\djangostock\venv\Scripts\python.exe
Python Version: 3.7.7
Python Path:
['C:\\Users\\JCW\\Desktop\\Stock_Market_Web_App-master\\stock',
'C:\\Users\\JCW\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
'C:\\Users\\JCW\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib',
'C:\\Users\\JCW\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
'C:\\Program Files\\Python37',
'C:\\djangostock\\venv',
'C:\\djangostock\\venv\\lib\\site-packages']
add_stock.html
<table class="table table-striped table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">TICKER</th>
<th scope="col">COMPANY</th>
<th scope="col">STK PRICE</th>
<th scope="col">PREV CLOSE</th>
<th scope="col">MARKET CAP</th>
<th scope="col">VOLUME</th>
<th scope="col">YTD CHG</th>
<th scope="col">52 WK HIGH</th>
<th scope="col">52 WK LOW</th>
<th scope="col">REMOVE STK</th>
</tr>
</thead>
<tbody>
{% if ticker %}
{% for list_item in output %}
<tr>
<th scope="row">{{ list_item.symbol }}</th>
<td>{{ list_item.companyName }}</td>
<td>${{ list_item.latestPrice }}</td/>
<td>${{ list_item.previousClose }}</td>
<td>${{ list_item.marketCap }}</td>
<td>{{ list_item.latestVolume }}</td>
<td>{{ list_item.ytdChange }}</td>
<td>${{ list_item.week52High }}</td>
<td>${{ list_item.week52Low }}</td>
<td><a href="{% url 'delete' item.id %}" class="btn btn-outline-danger btn-small">Delete {{ item }}</a></br></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% for item in ticker %}
<a href="{% url 'delete' item.id %}" class="btn btn-outline-danger btn-small">Delete {{ item }}</a>
{% endfor %}
views.py
def delete(request, stock_id):
item = Stock.objects.get(pk=stock_id) # call database by primary key for id #
item.delete()
messages.success(request, ("Stock Has Been Deleted From Portfolio!"))
return redirect('add_stock')
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('about.html', views.about, name="about"),
path('add_stock.html', views.add_stock, name="add_stock"),
path('delete/<stock_id>', views.delete, name="delete"),
path('news.html', views.news, name="news"),
]
What I've Tried I've tried using a for loop:
{% for item in ticker %}
<td><a href="{% url 'delete' item.id %}" class="btn btn-outline-danger btn-small">Delete {{ item }}</a></td>
{% endfor %}
But it loops through all the tickers, so there are delete buttons for all entries in the table on each row. If I don't use the for loop, I get the NoReverseMatch error. I know there is a way to solve this, but I have been working on it and searching for over two months.
I've also tried a while loop, and couldn't get it to work. I tried adding a delete form to the add_stock.html file with the corresponding request on the views.py file.
Links I've tried, but still couldn't solve:
- How to fix Django "NoReverseMatch" Error
- What is a NoReverseMatch error, and how do I fix it?
- NoReverseMatch: Reverse for 'deleted' with no arguments not found. - Django
- how to add delete button in each row of my table?
ments-not-found-django