gpt4 book ai didi

Django:需要手动呈现表单选项

转载 作者:行者123 更新时间:2023-12-04 18:56:09 24 4
gpt4 key购买 nike

我需要复制这个表格:

实时网页网址:

https://www.stickermule.com/products/die-cut-stickers

enter image description here

我一直在阅读有关如何手动呈现字段的文档,但无法手动呈现 radio 输入及其值。

https://docs.djangoproject.com/en/2.1/topics/forms/#rendering-fields-manually

我的问题特别是“选择数量部分”。

1)我的表单继承自 forms.ModelForm 并且只包含数量,不包含成本(以美元为单位),也不包含储蓄部分。

这就是为什么我需要将数量放在一个标签中,从表单中提取它,并在纯 html 中使用其他 2 个标签来手动放置成本和节省。

除非有办法将这些值作为其他字段放入模型中,但与字段数量(cantidad,西类牙语)相关。

所需的 HTML:

从这个页面复制我需要复制:

https://www.stickermule.com/products/die-cut-stickers

<form>
<div id="variants" class="product-option-group">
<legend>Select a size</legend>
<ul id="variant-options" class="options">
<li>
<input id="variant_79" name="variant_id" readonly="" type="radio" value="79">
<label for="variant_79"> 2" x 2"</label>
</li>
<li>
<input id="variant_78" name="variant_id" type="radio" value="78">
<label for="variant_78"> 3" x 3"</label>
</li>
<li>
<input id="variant_80" name="variant_id" type="radio" value="80">
<label for="variant_80"> 4" x 4"</label>
</li>
<li>
<input id="variant_81" name="variant_id" type="radio" value="81">
<label for="variant_81"> 5" x 5"</label>
</li>
<li>
<input id="variant_77" name="variant_id" type="radio" value="77">
<label for="variant_77"> Custom size</label>
</li>
</ul>
</div>
<div id="quantities">
<legend>"Select a quantity"</legend>
<ul>
<li>
<span class="table-cell">
<input type="radio">
<label>50</label>
</span>
<span id="price_50_id" class="table-cell price">$57</span>
<span class="table-cell savings"></span>
</li>
<li class=" quantity-item">
<span class="table-cell">
<input id="quantity_100" readonly="" name="quantity" type="radio" value="100">
<label for="quantity_100" class=" quantity"> 100</label>
</span>
<span id="price_100_id" class="table-cell price">$69</span>
<span class="table-cell savings">Save 39%</span>
</li>
</ul>
</div>

模型.py :
class TamaniosCantidades(models.Model):
TAMANIOS = (('2x2', '2" x 2"',), ('3x3', '3" x 3"',),
('4x4', '4" x 4"',), ('5x5', '5" x 5"',))

CANTIDADES = (('50', '50',), ('100', '100',),
('150', '150',))

tamanios = models.CharField(max_length=10, choices=TAMANIOS)
cantidades = models.CharField(max_length=10, choices=CANTIDADES)

forms.py :
class TamaniosCantidadesForm(forms.ModelForm):
tamanios = forms.ChoiceField(choices=TAMANIOS, widget=forms.RadioSelect(), label='Selecciona un tamaño')
cantidades = forms.ChoiceField(choices=CANTIDADES, widget=forms.RadioSelect(), label='Selecciona la cantidad')
class Meta:
model = TamaniosCantidades
fields = ['tamanios', 'cantidades']

def __str__(self):
return self.tamanios

我的 HTML:
<form action="/post_url_tamanioscantidades/" method="post">
{% csrf_token %}
{{ tamanioscantidades_form.as_p }}
<input type="submit" value="Submit"/>
</form>

总结:

我想将这些字段称为:
<form>
<div id="quantities">
<legend> {{ tamanioscantidades_form.label }} </legend>

<ul>
<li>
<span>
<input type="radio" id="{{ tamanioscantidades_form.input1_id }}>
<label> {{ tamanioscantidades_form.label }} </label>
</span>
<span>
<label>$57</label>
</span>
<span>
<label>""</label>
</span>
</li>
</ul>
</div>
</form>

更新 1:

根据我的回答,现在可以呈现单选按钮,但不能呈现标签。
class TamaniosCantidadesForm(forms.ModelForm):
tamanios = forms.ChoiceField(choices=TAMANIOS, widget=forms.RadioSelect(), label='Selecciona un tamaño')
cantidades = forms.ChoiceField(choices=CANTIDADES, widget=forms.RadioSelect(), label='Selecciona la cantidad')
class Meta:
model = TamaniosCantidades
fields = ['tamanios', 'cantidades']

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.cantidades_options = [option for option in self['cantidades']]

def __str__(self):
return self.tamanios

HTML:

第一个选项的标签 {{ tamanioscantidades_form.cantidades_options.0.label }}不会渲染
<ul>
<h1>{{ tamanioscantidades_form.cantidades.label }}</h1> #This renders
<li>
<span> {{ tamanioscantidades_form.cantidades_options.0.tag }}
<label> {{ tamanioscantidades_form.cantidades_options.0.label }} </label> #This does not render
</span>
</li>

最佳答案

如果我没记错RadioSelect模板中的小部件可以迭代。您可以在 documentation 中阅读更多相关信息。 .

您知道下面的方法不是首选方法。尽管如此,我还是提供了它,因为我不确定为什么您无法与成本和节省建立联系。

如果您提供有关数量、成本、节省的更多信息,我可能会提供帮助。

使用选项索引的解决方案

不幸的是,您所要求的内容无法开箱即用。但是通过这样的事情可以相当容易地完成。

class TamaniosCantidadesForm(forms.ModelForm):
tamanios = forms.ChoiceField(choices=TAMANIOS, widget=forms.RadioSelect(), label='Selecciona un tamaño')
cantidades = forms.ChoiceField(choices=CANTIDADES, widget=forms.RadioSelect(), label='Selecciona la cantidad')
class Meta:
model = TamaniosCantidades
fields = ['tamanios', 'cantidades']

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.cantidades_options = [option for option in self['cantidades']]

def __str__(self):
return self.tamanios

然后在您的模板中,您应该能够执行以下操作:
<form>
<div id="quantities">
<legend> {{ tamanioscantidades_form.cantidades.label }} </legend>

<ul>
<li>
<span>
{{ tamanioscantidades_form.cantidades_options.0.tag }}
<label> {{ tamanioscantidades_form.cantidades_options.0.choice_label }} </label>
</span>
<span>
<label>$57</label>
</span>
<span>
<label>""</label>
</span>
</li>
</ul>
</div>
</form>

关于Django:需要手动呈现表单选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53032806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com