提交后,下拉列表为空。表格在第二节课不起作用

2024-05-15 02:26:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我制作了一个模型表单,链接到模型中的一列。我提交表格后一切顺利。然而,当我想第二次添加更多的时候,我的股票下跌(choicefield)是空的。与我的渲染方式有关吗?或者是html问题?你知道吗

stock1

stock2

表格:

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'] 

你知道吗视图.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})

你知道吗型号.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)

模板:我认为我的模板可能是这里的问题,所以它是一个模态,也是一个表。在model中填写完数据后,一旦提交,它就会把数据放在表格中。但是当我点击模态来添加第二个条目时,我没有看到股票的下拉值。你知道吗

 {% 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">&times;</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 %}

Tags: divformeventdatevaluemybuysymbol

热门问题