简体   繁体   English

如何显示从 model 到 Django 模板的元组字段?

[英]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_choicesp.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.

相关问题 如何在Django模板中显示模型中的一个字段 - How to display one field from a model in Django template 从Django模板中,如何显示存储为BinaryField模型字段的图像? - From a django template, how to display images stored as BinaryField model field? Django - 如何在我的模型中有一个由整数元组组成的字段? - Django - how can I have a field in my model consisting of tuple of integers? 如何从django模板中的模型和字段中获取模型字段值? - How to get model field value from model and field in django template? Django-在模板上显示模型中字段的过滤后总和 - Django - Display filtered sum of a field from a model on template 如何从Django模板中的模型访问函数? - How can I access a function from model in template in Django? 如何将媒体目录中的图像显示到模板中? Django的 - How can I display images from media directory into template? Django 如何为Django模型中的字段元组指定唯一性 - How to specify uniqueness for a tuple of field in a Django model Django 1.6:无法显示模板中模型的图片。 - Django 1.6: Can't display a picture from a model in template. 如何制作 Django model 表单,其字段名称与 model 字段名称不同? - how can I make a Django model form with a field name in the form different from the model field name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM