我制作了一个模型表单,该模型链接到模型中的列。提交表格后,一切运行顺利。但是,当我想第二次添加更多时,我的股票下拉列表(choicefield)为空。它与我的渲染方式有关吗?还是html问题?
表格:
class My_Folio(forms.ModelForm):
symbol = forms.ChoiceField(choices=coin_choices)
class Meta:
model = my_data
widgets = {'event_date': forms.DateInput(attrs={'class':'datepicker'})}
fields = ['symbol','buy_price','quantity','event_date']
views.py
@login_required
def blockfolio(request):
my_coin = my_data.objects.filter(user=request.user).order_by('symbol')
if request.method == 'POST':
my_folio = My_Folio(request.POST)
if my_folio.is_valid():
instance = my_folio.save(commit=False)
instance.user = request.user
instance.symbol = my_folio.cleaned_data['symbol']
instance.save()
return render(request,'blockfolio/blockfolio.html', {'my_coin':my_coin})
else:
messages.error(request, "Error")
else:
my_folio = My_Folio()
return render( request, 'blockfolio/blockfolio.html', {'my_folio':my_folio})
model.py
class my_data(models.Model):
symbol = models.CharField(max_length = 20)
buy_price = models.DecimalField(max_digits =30, decimal_places = 10)
quantity= models.DecimalField(max_digits =30, decimal_places = 10)
total_value = models.DecimalField(max_digits =30, decimal_places = 10)
user = models.ForeignKey(User, on_delete= models.CASCADE)
event_date = models.DateField(blank =False, default=datetime.date.today)
模板:我认为我的模板可能是这里的问题,所以它既是模态又是表格。在以模式形式填写内容之后,一旦提交,它将以表格形式存储该数据。但是,当我单击模式以添加第二个条目时,我没有看到Stock的下拉值。
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Trade</h4>
</div>
<div class="modal-body">
<form width="600px" action="." method="post" oninput="total_value.value= quantity.value * buy_price.value" >{% csrf_token %}
<div class="row align-items-start">
<div class="col-sm-5">
<label for="symbol">Stocks</label>
<select class="form-control" id="symbol" name ="symbol">
{% for key,choice in my_folio.symbol.field.choices %}
<option >{{key}}</option>
{% endfor%}
</select>
{{ my_folio.errors }}
{{ my_folio.non_field_errors }}
</div>
<div class="form-group">
<label for="quantity">Enter your value</label>
<input type="number" class="form-control" name="quantity" id="quantity">
</div>
<div class="form-group">
<label name ="event_date" id="event_date" for="event_date">Enter your value</label>
{{my_folio.event_date}}
</div>
<div class="form-group">
<label for="buy_price">buy price</label>
<input type="number" class="form-control valid" name="buy_price" maxlength="20" id ="buy_price">
</div>
<div class="form-group">
<label for="total_value">Total</label>
=<output type = "number" name="instance.total_value" id= "total_value" for ="quantity buy_price"></output>
</div>
</div>
<button type="submit">Submit</button>
</form>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
</div>
<div class="cointainer">
<table class = "table">
<tr class = " d-none d-sm-table-row">
<th colspan="2">ASSET</th>
<th class="text-right"> Quantity </th>
<th class="text-right"> Buy Price </th>
<th class="text-right"> Investment </th>
<th class="text-right"> Date </th>
</tr>
<tbody>
{% for item in my_coin %}
<tr class="d-none d-sm-table-row">
<td colspan="2"> {{item.symbol}}</td>
<td class="text-right"> {{item.quantity}}</td>
<td class="text-right"> {{item.buy_price}}</td>
<td class="text-right"> {{item.total_value}}</td>
<td class="text-right"> {{item.event_date}}</td>
</tr>
{% endfor %}
<tr>
<td colspan="3">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong">Trade</button>
</td>
</tr>
</tbody>
</table>
</div>
{% endblock content %}