社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

如何在django中保存多个克隆表单?

Sam • 3 年前 • 332 次点击  

意见。py

@login_required
def add_product(request):
   productform = ProductForm()
   variantsform = VariantsForm()
   productvariantsform = ProductVariantsForm()
     if request.method == 'POST':
       productform = ProductForm(request.POST, request.FILES)
       variantsform = VariantsForm(request.POST, request.FILES)
       productvariantsform = ProductVariantsForm(request.POST, request.FILES)

       if productform.is_valid():
           product = productform.save(commit=False) 
           vendor = CustomUser.objects.filter(id=request.user.id)
           print(vendor)
           product.vendor = vendor[0]
           product.slug = slugify(product.product_name)
           product.save() 
       if variantsform.is_valid():
           variantsform.save() #finally save

       if productvariantsform.is_valid():
          productvariantsform.save() #finally save
          return redirect('vendor:vendor-admin')
       else:
          return HttpResponse("Nothing submitted...")
    else:
        productform = ProductForm
        variantsform = VariantsForm()
        productvariantsform = ProductVariantsForm()
    return render(request, 'vendor/add_product.html',
            {'productform': productform, 'variantsform':variantsform, 'productvariantsform': productvariantsform})

添加产品。html

<div class="container mt-5" id="p"> 
<h1 class="title">Add Product</h1>
<div class="card" style="width: 38rem;">
<form method="POST" action="{% url 'vendor:add-product' %}" enctype="multipart/form-data">
    
  {% csrf_token %}

  <div class="row">
    <div class="col-lg-12 col-md-12">
      {{ productform.vendorid|as_crispy_field }}
    </div>
    <div class="col-lg-12 col-md-12">
      <a id="vendor_id_search" class="btn btn-info mt-4">search</a>
    </div>
  </div>      
  <div id="show_vendorname">

  </div><br>

  {{ productform.maincategory|as_crispy_field }}
  {{ productform.productcategory|as_crispy_field }}
  {{ productform.subcategory|as_crispy_field }}
  {{ productform.product_name|as_crispy_field }}
  {{ productform.brand_name |as_crispy_field }}

  <br>
  <div>
    <p></p>
  </div>
  <hr>

  <div id="order-details-booking">
    
    <div class="row">
      <div class="form-holder">
        <div class="col">
          <h1>Variant Form</h1>
          {{ variantsform.variant_type|as_crispy_field}}
          {{ variantsform.variant_value|as_crispy_field}}        
          {{ productvariantsform.price|as_crispy_field}} 
          {{ productvariantsform.initial_stock|as_crispy_field}}
          {{ productvariantsform.weight_of_product|as_crispy_field}} 

        
          <div class="input-group mb-3">
            <label class="input-group-text" for="upload_file">Upload Images</label>
            <input type="file" id="upload_file" onchange="preview_image();" multiple class="form-control">
          </div>
          <div id="image_preview"></div>
          {% comment %} {{ productvariantsform.images|as_crispy_field}}  {% endcomment %}
          {{ variantsform.sleeve|as_crispy_field }}
          {{ variantsform.material|as_crispy_field }}
          {{ variantsform.neck|as_crispy_field }}
          <div class="col s1">
            <a class="btn btn-outline-danger remove">- remove this</a>
          </div>
        </div>
      </div>     
    </div>

    <div class="form-holder-append"></div>
    <div class="row">
      <div class="col-xs-4">
        <a class="btn btn-outline-success add">+ add more variants</a>
      </div>
    </div>
  </div><br>

  <hr>

  <div class="field mt-4">
    <div class="control">
      <button type="submit" class="button is-dark is-uppercase">Add Product</button>
    </div>
  </div>
</form>
 <script>    
     // Cloning Form
     var id_count = 1;
     $('.add').on('click', function() {
     var source = $('.form-holder:first'), clone = source.clone();
     clone.find(':input').attr('id', function(i, val) {
       return val + id_count;
     });
     clone.appendTo('.form-holder-append');
      id_count++;
      });

    // Removing Form Field
     $('body').on('click', '.remove', function() {
       var closest = $(this).closest('.form-holder').remove();
    });

  </script>

对保存不同类型的表格有疑问。 我有三张表格。产品表单,来自的变体,产品变体表单。

在模板中,我已经展示了。这里我克隆了一个表单,它包含variantsform和productvariantsform的混合字段。

在视图中。py我试着用django分别保存每个表单 if form.is_valid() form.save() 方法没用。需要指导才能在视图中正确处理此问题。皮耶。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133217
 
332 次点击