Python 版本 3.7.7 和 Django 版本 2.2.3。 Github 上的代码 https://github.com/jcwill415/Stock_Market_Web_App
我想在表格的最后一列中添加一个删除按钮。当代码在表之外时,它可以从表中删除一个条目。但是当代码在表内时,我收到一个 NoReverseMatch 错误,上面写着:
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 %}
视图.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')
网址.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"),
]
我尝试过 的我尝试过使用 for 循环:
{% for item in ticker %}
<td><a href="{% url 'delete' item.id %}" class="btn btn-outline-danger btn-small">Delete {{ item }}</a></td>
{% endfor %}
但它会循环遍历所有代码,因此每行的表格中的所有条目都有删除按钮。如果我不使用 for 循环,我会收到 NoReverseMatch 错误。我知道有办法解决这个问题,但我一直在努力并寻找两个多月。
我也尝试了一个while循环,但无法让它工作。我尝试将删除表单添加到 add_stock.html 文件中,并在 views.py 文件中添加相应的请求。
我尝试过的链接,但仍然无法解决:
- 如何修复 Django“NoReverseMatch”错误
- 什么是 NoReverseMatch 错误,我该如何解决?
- NoReverseMatch:对未找到任何参数的“已删除”进行反转。- 姜戈
- 如何在我的表格的每一行中添加删除按钮?
ment-not-found-django