问题描述
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 %}
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"),]
我尝试过的事情 我已经尝试过使用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循环,但无法正常工作。我尝试将一个删除表单添加到具有对views.py文件的相应请求的add_stock.html文件中。
- 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
解决方法
您在2个不同的地方使用delete
。首先,您引用了错误的项目:
{% for list_item in output %}
...
{% url 'delete' item.id %}
应该是
{% url 'delete' list_item.id %}
在第二个中,这是正确的:
{% for item in ticker %}
<a href="{% url 'delete' item.id %}"
此外,在许多情况下,delete
是保留字。我认为情况并非如此,但仍然是个坏习惯。随处更改为delete_stock
:
<a href="{% url 'delete_stock' list_item.id %}" ...>
def delete_stock(request,stock_id=None):
path('delete_stock/<stock_id>',views.delete_stock,name="delete_stock"),
stock_id
是整数还是字符串?要找出答案,请在add_stock()
的循环内打印:
api = json.loads(api_request.content)
print(api)
output.append(api)
此外,库存ID将作为字符串出现在url中,因此,如果它是整数,请执行以下操作:
item = Stock.objects.get(pk=int(stock_id))
或使用:
path('delete_stock/<int:stock_id>',
其他网址有效吗?
奖金提示:
您的网址中无需包含.html
。看起来过时了。更好的形式是:
urlpatterns = [
path('',views.home,name="home"),path('about',views.about,name="about"),path('add_stock',views.add_stock,name="add_stock"),path('delete_stock/<int:stock_id>',path('news',views.news,name="news"),]