[英]How can I display a tuple field from a model to a template in Django?
I'm pretty new to Django here guys so go easy on me please...我对 Django 很陌生,伙计们,所以 go 请放轻松...
Let me elaborate on what the title question says.让我详细说明一下标题问题的含义。 Basically I have this model....基本上我有这个 model....
class Meta:
verbose_name_plural = 'Digital Media'
CATEGORY_CHOICES = (
('icon_sets', 'Icon Sets'),
('brand_logos', 'Brand Logos'),
('web_banners', 'Web Banners')
)
name = models.CharField(max_length=20, choices=CATEGORY_CHOICES)
SIZE_CHOICES = (
('1616', '16 x 16 pixels'),
('3232', '32 x 32 pixels'),
('6464', '64 x 64 pixels'),
('128128', '128 x 128 pixels'),
('256256', '256 x 256 pixels')
)
sizes = models.CharField(max_length=20, choices=SIZE_CHOICES)
def __str__(self):
return self.name
and this view...而这个观点...
def product_detail(request, product_id):
""" A view to show individual product details """
print_media = Print_Media.objects.all()
digital_media = Digital_Media.objects.all()
product = get_object_or_404(Product, pk=product_id)
print(product, print_media, digital_media)
context = {
'product': product,
'print_media': print_media,
'digital_media': digital_media,
}
return render(request, 'products/product_detail.html', context)
So "IF" all is ok with the above code, can someone help me to get the field "sizes" from the model onto a template as I'm having trouble doing this on my own - here is what I have tried so far...所以“如果”上面的代码一切正常,有人可以帮我将 model 中的字段“大小”放到模板上,因为我自己做这件事时遇到了麻烦 - 这是我到目前为止所尝试的。 ..
{% with product.is_print_media as p %}
{% if p %}
<div class="col-12">
{{ p.sizes }}
<p><strong>Size:</strong></p>
<select class="form-control rounded-0 w-50" name="product_size" id="id_product_size">
<option value="{{ p.sizes }}"></option>
<option value=""></option>
<option value="" selected></option>
<option value=""></option>
<option value=""></option>
</select>
</div>
{% endif %}
{% endwith %}
Any help with this is much appreciated:)对此的任何帮助都非常感谢:)
Again - go easy on the newbie.....再次 - go 对新手很容易.....
If SIZE_CHOICES is attribute of model Print_Media
you can access it us p.SIZE_CHOICES
in template.如果SIZE_CHOICES是 model Print_Media
的属性,您可以在模板中使用p.SIZE_CHOICES
访问它。
Otherwise, provide it as context
in your view Digital_Media.SIZE_CHOICES
:否则,在您的视图Digital_Media.SIZE_CHOICES
中将其作为context
提供:
def product_detail(request, product_id):
""" A view to show individual product details """
print_media = Print_Media.objects.all()
digital_media = Digital_Media.objects.all()
product = get_object_or_404(Product, pk=product_id)
print(product, print_media, digital_media)
context = {
'product': product,
'print_media': print_media,
'digital_media': digital_media,
'category_choices': Digital_Media.CATEGORY_CHOICES 'size_choices': Digital_Media.SIZE_CHOICES
}
return render(request, 'products/product_detail.html', context)
In template size_choices
or p.SIZE_CHOICES
.在模板size_choices
或p.SIZE_CHOICES
中。 Assuming that you used size_choices
:假设您使用size_choices
:
{% with product.is_print_media as p %}
{% if p %}
<div class="col-12">
{{ p.sizes }}
<p><strong>Size:</strong></p>
<section class="mt-20">
<select class="form-control rounded-0 w-50" name="product_size" id="id_product_size_{{ p.pk }}">
{% for size in size_choices %}
<option value="{{ item.0 }}" {% if size.0 == p.sizes %}selected{% endif %}>{{ item.1 }}</option>
{% endfor %}
</select>
</div>
{% endif %}
{% endwith %}
In django templates, choices will be showing like this.在 django 模板中,选项将像这样显示。 That will return the readable value for the field,这将返回该字段的可读值,
{{ p.get_sizes_display }}. {{ p.get_sizes_display }}。
Vote up, if this one helps you !!!投票,如果这个对你有帮助!!!
For every field that has choices set, the object will have a get_FOO_display() method, where FOO is the name of the field.对于设置了选项的每个字段,object 将有一个 get_FOO_display() 方法,其中 FOO 是字段的名称。 This method returns the “human-readable” value of the field.此方法返回字段的“人类可读”值。 so try:所以试试:
{{p.get_sizes_display}}
for another example:另一个例子:
from django.db import models
class Person(models.Model):
SHIRT_SIZES = (
('S', 'Small'),
('M', 'Medium'),
('L', 'Large'),
)
name = models.CharField(max_length=60)
shirt_size = models.CharField(max_length=2, choices=SHIRT_SIZES)
we can access Small, Medium and Large in templates with (p as a Person):我们可以使用 (p as a Person) 在模板中访问 Small、Medium 和 Large:
{{p.get_shirt_size_display}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.