Python 如何使用Django删除表中的行
我是Django的新手。我正在尝试添加删除按钮以删除该行。但是,输出不是我想要的,因为它输出多次,因为我创建了一个循环来获取其他列的数据。出于这个原因,有没有一种解决方案可以让delete按钮在循环之外运行 add_stock.htmlPython 如何使用Django删除表中的行,python,django,Python,Django,我是Django的新手。我正在尝试添加删除按钮以删除该行。但是,输出不是我想要的,因为它输出多次,因为我创建了一个循环来获取其他列的数据。出于这个原因,有没有一种解决方案可以让delete按钮在循环之外运行 add_stock.html {% extends 'base.html' %} {% block content %} <h1>Add Stock</h1> <form action="{% url 'add_stock' %}" cl
{% extends 'base.html' %}
{% block content %}
<h1>Add Stock</h1>
<form action="{% url 'add_stock' %}" class="form-inline my-2 my-lg-0" method='POST'>
{% csrf_token %}
<input class="form-control me-2" type="search" placeholder="Add Stock" aria-label="Search" name="ticker">
<button class="btn btn-outline-secondary my-2 my-sm-0" type="submit">Add Stock</button>
</form>
<br/>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th scope="col">Symbol</th>
<th scope="col">Company Name</th>
<th scope="col">Last</th>
<th scope="col">Extended Hours</th>
<th scope="col">Change</th>
<th scope="col">% Change</th>
<th scope="col">Volume</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% if ticker %}
{% for list_item in output %}
{% for item in ticker %}
<tr>
<th scope="row"> {{ list_item.symbol }} </th>
<td>{{ list_item.companyName }}</td>
<td>${{ list_item.latestPrice }}</td>
<td>${{ list_item.extendedPrice }}</td>
<td>{{ list_item.change }}</td>
<td>{{ list_item.changePercent }}%</td>
<td>{{ list_item.volume }}</td>
<td><a href=" {% url 'delete' item.id %} "> Delete </a></td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>
{% endif %}
我将创建一个包含两个元组的列表,而不是两个列表(ticker
和output
),这样您只需在模板中的单个列表上进行迭代
views.py
from django.shortcuts import render, redirect
from .models import Stock
from django.contrib import messages
from .forms import StockForm
def home(request):
import requests
import json
if request.method == 'POST':
ticker = request.POST['ticker']
# pk_1c2f5aec07a04049afc7200c61b6b1f3
api_request = requests.get("https://cloud.iexapis.com/stable/stock/" + ticker + "/quote?token=pk_1c2f5aec07a04049afc7200c61b6b1f3")
try:
api = json.loads(api_request.text)
except Exception as e:
api = 'Error...'
return render(request, 'home.html', {'api': api})
else:
return render(request, 'home.html', {'ticker': 'Enter a symbol...'})
# return render(request, 'home.html', {'api': api})
def about(request):
return render(request, 'about.html', {})
def add_stock(request):
import requests
import json
if request.method == 'POST':
form = StockForm(request.POST or None)
if form.is_valid():
form.save()
messages.success(request, ('Stock added'))
return redirect('add_stock')
else:
ticker = Stock.objects.all()
output = []
for ticker_item in ticker:
api_request = requests.get("https://cloud.iexapis.com/stable/stock/" + str(ticker_item) + "/quote?token=pk_1c2f5aec07a04049afc7200c61b6b1f3")
try:
api = json.loads(api_request.text)
output.append(api)
except Exception as e:
api = 'Error...'
return render(request, 'add_stock.html', {'ticker': ticker, 'output': output})
def delete(request, stock_id):
item = Stock.objects.get(pk=stock_id)
item.delete()
messages.success(request, ('Stock has deleted'))
return redirect(add_stock)
def add_stock(request):
import requests
import json
if request.method == 'POST':
form = StockForm(request.POST or None)
if form.is_valid():
form.save()
messages.success(request, ('Stock added'))
return redirect('add_stock')
else:
ticker = Stock.objects.all()
items = []
for ticker_item in ticker:
api_request = requests.get("https://cloud.iexapis.com/stable/stock/" + str(ticker_item) + "/quote?token=pk_1c2f5aec07a04049afc7200c61b6b1f3")
try:
api = json.loads(api_request.text)
output.append(api)
except Exception as e:
api = 'Error...'
items.append((ticker, api))
return render(request, 'add_stock.html', {'items': items})
add_stock.html的相关部分:
{% for item, list_item in items %}
<tr>
<th scope="row"> {{ list_item.symbol }} </th>
<td>{{ list_item.companyName }}</td>
<td>${{ list_item.latestPrice }}</td>
<td>${{ list_item.extendedPrice }}</td>
<td>{{ list_item.change }}</td>
<td>{{ list_item.changePercent }}%</td>
<td>{{ list_item.volume }}</td>
<td><a href=" {% url 'delete' item.id %} "> Delete </a></td>
</tr>
{% endfor %}
{%用于项目,列出项目%}
{{list_item.symbol}
{{list_item.companyName}
${{list_item.latestPrice}
${{list_item.extendedPrice}}
{{list_item.change}
{{list_item.changePercent}}%
{{list_item.volume}
{%endfor%}
{% for item, list_item in items %}
<tr>
<th scope="row"> {{ list_item.symbol }} </th>
<td>{{ list_item.companyName }}</td>
<td>${{ list_item.latestPrice }}</td>
<td>${{ list_item.extendedPrice }}</td>
<td>{{ list_item.change }}</td>
<td>{{ list_item.changePercent }}%</td>
<td>{{ list_item.volume }}</td>
<td><a href=" {% url 'delete' item.id %} "> Delete </a></td>
</tr>
{% endfor %}