如何在Django模板中使用按钮来添加更多表单变量?那么如何在视图中处理多个表单呢?
我有两个问题:如何在Django模板中使用按钮来添加更多表单变量?那么如何在视图中处理多个表单呢?,django,django-forms,django-templates,django-views,Django,Django Forms,Django Templates,Django Views,我有两个问题: 如何对按钮进行编码,以便能够在模板中添加1个或多个重复的表单变量?我想创建一个产品页面,其中产品可能有多种颜色和厚度 那么如何在views.py中使用多个颜色和厚度将产品保存到数据库中?对于循环,我是否需要 models.py中的我的4个相关模型: class Product(models.Model): product_id = models.CharField(max_length=6) video = models.URLField(max_length=2
产品
页面,其中产品
可能有多种颜色
和厚度
李>
views.py
中使用多个颜色和厚度将产品
保存到数据库中?对于
循环,我是否需要
models.py
中的我的4个相关模型:
class Product(models.Model):
product_id = models.CharField(max_length=6)
video = models.URLField(max_length=250, null=True, blank=True)
class ColorParent(models.Model):
name = models.CharField(max_length=50)
class ProductColor(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
color = models.ForeignKey(ColorParent, on_delete=models.CASCADE)
class ProductThickness(models.Model, MeasurementUnits):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
dimension = models.PositiveIntegerField(verbose_name='Thickness in International SI unit',
null=True, blank=True)
dimension_unit = models.CharField(max_length=20,
choices=MeasurementUnits.si_base_units,
default=MeasurementUnits.millimetre, null=True, blank=True)
alias = models.CharField(verbose_name='Thickness in Imperial unit',
max_length=10, null=True, blank=True)
alias_unit = models.CharField(max_length=20,
choices=MeasurementUnits.imperial_base_units,
default=MeasurementUnits.inch, null=True, blank=True)
class ProductForm(ModelForm):
class Meta:
model = Product
fields = ['product_id', 'video']
class ColorParentForm(ModelForm):
class Meta:
model = ColorParent
fields = ['name']
class ProductColorForm(ModelForm):
class Meta:
model = ProductColor
fields = ['color']
class ProductThicknessForm(ModelForm):
class Meta:
model = ProductThickness
fields = ['dimension', 'dimension_unit',
'alias', 'alias_unit']
def product_create_view(request):
product_form = ProductForm(request.POST, prefix='product_form')
color_form = ProductColorForm(request.POST, prefix='color_form')
thickness_form = ProductThicknessForm(request.POST, prefix='thickness_form')
if product_form.is_valid() and color_form.is_valid() and thickness_form.is_valid():
product = product_form.save() # save parent modelform first.
color = color_form.save(commit=False) # do not commit immediately for child modelform.
thickness = thickness_form.save(commit=False)
color.product = product # pair the color with created product first.
color.save() # proceed to save.
thickness.product = product
thickness.save()
context = {
'product_form': product_form,
'color_form': color_form,
'thickness_form': thickness_form,
}
return render(request, 'product_visualizers/product_create.html', context)
我的表单.py
:
class Product(models.Model):
product_id = models.CharField(max_length=6)
video = models.URLField(max_length=250, null=True, blank=True)
class ColorParent(models.Model):
name = models.CharField(max_length=50)
class ProductColor(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
color = models.ForeignKey(ColorParent, on_delete=models.CASCADE)
class ProductThickness(models.Model, MeasurementUnits):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
dimension = models.PositiveIntegerField(verbose_name='Thickness in International SI unit',
null=True, blank=True)
dimension_unit = models.CharField(max_length=20,
choices=MeasurementUnits.si_base_units,
default=MeasurementUnits.millimetre, null=True, blank=True)
alias = models.CharField(verbose_name='Thickness in Imperial unit',
max_length=10, null=True, blank=True)
alias_unit = models.CharField(max_length=20,
choices=MeasurementUnits.imperial_base_units,
default=MeasurementUnits.inch, null=True, blank=True)
class ProductForm(ModelForm):
class Meta:
model = Product
fields = ['product_id', 'video']
class ColorParentForm(ModelForm):
class Meta:
model = ColorParent
fields = ['name']
class ProductColorForm(ModelForm):
class Meta:
model = ProductColor
fields = ['color']
class ProductThicknessForm(ModelForm):
class Meta:
model = ProductThickness
fields = ['dimension', 'dimension_unit',
'alias', 'alias_unit']
def product_create_view(request):
product_form = ProductForm(request.POST, prefix='product_form')
color_form = ProductColorForm(request.POST, prefix='color_form')
thickness_form = ProductThicknessForm(request.POST, prefix='thickness_form')
if product_form.is_valid() and color_form.is_valid() and thickness_form.is_valid():
product = product_form.save() # save parent modelform first.
color = color_form.save(commit=False) # do not commit immediately for child modelform.
thickness = thickness_form.save(commit=False)
color.product = product # pair the color with created product first.
color.save() # proceed to save.
thickness.product = product
thickness.save()
context = {
'product_form': product_form,
'color_form': color_form,
'thickness_form': thickness_form,
}
return render(request, 'product_visualizers/product_create.html', context)
我的视图.py
:
class Product(models.Model):
product_id = models.CharField(max_length=6)
video = models.URLField(max_length=250, null=True, blank=True)
class ColorParent(models.Model):
name = models.CharField(max_length=50)
class ProductColor(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
color = models.ForeignKey(ColorParent, on_delete=models.CASCADE)
class ProductThickness(models.Model, MeasurementUnits):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
dimension = models.PositiveIntegerField(verbose_name='Thickness in International SI unit',
null=True, blank=True)
dimension_unit = models.CharField(max_length=20,
choices=MeasurementUnits.si_base_units,
default=MeasurementUnits.millimetre, null=True, blank=True)
alias = models.CharField(verbose_name='Thickness in Imperial unit',
max_length=10, null=True, blank=True)
alias_unit = models.CharField(max_length=20,
choices=MeasurementUnits.imperial_base_units,
default=MeasurementUnits.inch, null=True, blank=True)
class ProductForm(ModelForm):
class Meta:
model = Product
fields = ['product_id', 'video']
class ColorParentForm(ModelForm):
class Meta:
model = ColorParent
fields = ['name']
class ProductColorForm(ModelForm):
class Meta:
model = ProductColor
fields = ['color']
class ProductThicknessForm(ModelForm):
class Meta:
model = ProductThickness
fields = ['dimension', 'dimension_unit',
'alias', 'alias_unit']
def product_create_view(request):
product_form = ProductForm(request.POST, prefix='product_form')
color_form = ProductColorForm(request.POST, prefix='color_form')
thickness_form = ProductThicknessForm(request.POST, prefix='thickness_form')
if product_form.is_valid() and color_form.is_valid() and thickness_form.is_valid():
product = product_form.save() # save parent modelform first.
color = color_form.save(commit=False) # do not commit immediately for child modelform.
thickness = thickness_form.save(commit=False)
color.product = product # pair the color with created product first.
color.save() # proceed to save.
thickness.product = product
thickness.save()
context = {
'product_form': product_form,
'color_form': color_form,
'thickness_form': thickness_form,
}
return render(request, 'product_visualizers/product_create.html', context)
我当前的product\u create.html
模板:
<form action="." method="post" enctype="multipart/form-data"> {% csrf_token %}
<p>Product ID {{product_form.product_id }}</p>
<p>Color Parent {{color_form.color}}</p> # Button target to add more {{color_form.color}}
<p>Video URL {{product_form.video}}</p>
<h3>Technical Specifications</h3>
<h4>Thickness Options</h4>
<p> # Button target to add more {{thickness_form.*}}
Dimension: {{thickness_form.dimension}} {{thickness_form.dimension_unit}}
Alias: {{thickness_form.alias}} {{thickness_form.alias_unit}}
</p>
<p><input type="submit" value="Save" /></p>
</form>
{%csrf\u令牌%}
产品ID{{Product_form.Product_ID}
颜色父项{Color\u form.Color}#按钮目标以添加更多{Color\u form.Color}
视频URL{{product_form.Video}
技术规格
厚度选项
#按钮目标添加更多{{thickness_form.*}
维度:{{厚度\形式.维度}{{厚度\形式.维度\单位}
别名:{{thickness\u form.Alias}}{{thickness\u form.Alias\u unit}