猿问

为什么我得到空表格?如何自动创建 Iniline 模型?

有2 forms一个页面上。


有2种型号:1 Product.. 2. SpeciallyPrice. SpeciallyPrice通过FK链接到Product. 同时,SpeciallyPrice是Inline 模型中的Product.


的字段SpecialPriceForm是使用JS自动创建的。也就是说,它们可能是第n个数字。有必要为每个创建的字段创建一个记录。原则上,我猜怎么做 - 使用循环来驱动获得的值。但问题是由于某种原因None来自form. 请帮我。


模板+js


<html><body>

Special price from {{ special_form.adittional_specially_price }} kg {{ special_form.adittional_specially_number }} usd


    <script>

        (function(){

            var copy = document.querySelector('.field.inline.specially').cloneNode(true);

            document.querySelector('html').addEventListener('input', function(e){

                if(e.target.classList.contains('event') && e.target.tagName == 'INPUT'){

                    var error = 0;

                    for(var evt of document.querySelectorAll('.field.inline.specially input.event')){

                        evt.value = evt.value.replace(/[^\d]/,'');

                        if(!evt.value || +evt.value < 1) error++;

                    }

                    if(!error){

                        var last = document.querySelectorAll('.field.inline.specially');

                        last[last.length-1].insertAdjacentHTML('afterEnd', copy.outerHTML);

                    }

                }

            });

        })();

    </script>

</body></html>

这个表格我在视图中看到,当打印form检查时


<label for="specially" class="subhead">Special price from</label>

<span class="id_specially_price"><input type="text" name="adittional_specially_price" style="width: 165px" class="event" id="id_adittional_specially_price"></span>

<span>kg</span>

<span class="id_specially_number"><input type="text" name="adittional_specially_number" style="width: 100px" class="event" id="id_adittional_specially_number"></span>

<span>usd</span>

我查看了视图- 表单在那里呈现,但只有一个字段,而不是所有字段。而形式是空的。如何解决这个问题?也许应该连接Ajax并以某种方式处理请求?或者有更多Django 的选择吗?


慕婉清6462132
浏览 167回答 1
1回答

鸿蒙传说

回答这一点评论:“真的来了最后两个用 JS 字段创建的空。如何让所有字段都来,请告诉我?”要在 CBV 中保存内联表单集:def form_valid(self, form):&nbsp; &nbsp; context = self.get_context_data()&nbsp; &nbsp; inline_form = context['inline_form']&nbsp; &nbsp; if inline_form.is_valid():&nbsp; &nbsp; &nbsp; &nbsp; self.object = form.save()&nbsp; &nbsp; &nbsp; &nbsp; inline_form.instance = self.object&nbsp; &nbsp; &nbsp; &nbsp; inline_form.save()显然,您必须inline_form在context.
随时随地看视频慕课网APP

相关分类

Python
我要回答