Access Model CharField() Choices in HTML

Hello Pals.
I have a CharField() With Choices in my model and i want to access the values inside it to insert it in my html
in a Filter Dropdown with Each Value.
HTML

<div class="eight wide column">
    <div class="ui compact menu">
        <div class="ui simple dropdown item FilterByCategory">
            Filter
            <i class="filter icon"></i>
            <div class="menu">
            {% for choice in product.get_tag_display %}
                <div class="item">
                    <a href=""> {{ choice.value }} </a>
                </div>
            {% endfor %}
            </div>
        </div>
    </div>
</div>

Model:

class Product(models.Model):
	TagsChoices = (
		(_('featured'), _('Featured')),
		(_('sale'), _('Sale')),
		(_('clearance'), _('Clearance')),
		(_('pre_order'), _('Pre Order')),
		(_('out_of_stock'), _('Out Of Stock')),
		(_('best_seller'), _('Best Seller')),
	)
	category = models.ForeignKey(Category, verbose_name=_('Product Category'), 
        related_name=_('product_category'), on_delete=models.CASCADE)
	tag = models.CharField(verbose_name=_('Product Tag'), choices=TagsChoices, max_length=20, null=True, 
        blank=True)

is there anyway better or more useful.

Your TagsChoices are an attribute of the Product class. That means you have access to it just like any other attribute of any other class.

If product is an instance of Product, then product.TagsChoices is a reference to the tuple of two-tuples.

e.g.: product.TagsChoices[1][0] == _('sale')

1 Like

thanks Ken, yes its working in terminal, but in my html it don’t get any values.
and products already rendered in my category and i render it in this html page.

html

<div class="ui container">

	<div class="ui grid">
		<div class="column">
			<img class="ui centered image CategoryLandScapeImage" src="{{ category.image.url }}"
			alt="Category Image Alt Text.">
		</div>
	</div>

	{% include '__Snips/__Divider.html' %}

	<div class="ui center aligned grid">
		<div class="sixteen wide column">
			<div class="ui breadcrumb">
				{% if category.catalog %}
				<a class="section" href="{{ category.catalog.get_absolute_url }}">{{ category.catalog.name }}</a>
				<i class="right angle icon divider"></i>
				{% endif %}
				<a class="section" href="{{ category.get_absolute_url }}">{{ category.name }}</a>
				<i class="right angle icon divider"></i>
			</div>
		</div>
		<div class="eight wide column">
			<div class="ui compact menu">
				<div class="ui simple dropdown item FilterByCategory">
					Filter
					<i class="filter icon"></i>
					<div class="menu">
					{% for tag in product.TagsChoices %}
						<div class="item">
							<a href="">{{ tag[1] }}</a>
						</div>
					{% endfor %}
					</div>
				</div>
			</div>
		</div>
		<div class="eight wide column">
			<div class="ui compact menu">
				<div class="ui simple dropdown item SortByCategory">
					Sort
					<i class="sort icon"></i>
					<div class="menu">
						<div class="item">
							<a href="">
								Alphabet A to Z
								<i class="sort alphabet down large icon"></i>
							</a>
						</div>
						<div class="item">
							<a href="">
								Alphabet Z to A
								<i class="sort alphabet up icon"></i>
							</a>
						</div>
						<div class="item">
							<a href="">
								Price Highest to Lowest 
								<i class="sort amount down icon"></i>
							</a>
						</div>
						<div class="item">
							<a href="">
								Price Lowest to Highest
								<i class="sort amount up icon"></i>
							</a>							
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

	<div class="ui grid">
		{% for product in products %}
		<div class="four wide column">
			<div class="ui basic segment">
				<div class="ui raised link card">
					<div class="image">
						{% for image in product.product_image.all %}
						{% if image.thumbnail %}
						<img class="ui fluid rounded image PrdctImgCtgry" src="{{ image.image.url }}">
						{% endif %}
						{% if product.tag %}
						{% include '__Snips/__Tag_Ribbon.html' %}
						{% endif %}
						{% endfor %}
					</div>
					<div class="content">
						<div class="header">{{ product.name }}</div>
						<div class="description">
							{{ product.sku }}
						</div>
					</div>
					<div class="extra content">
						{% if product.before_price %}
						<span>
							<i class="money bill alternate outline icon"></i>
							<del class="DublStrk">{{ product.before_price }}</del>
						</span>
						{% endif %}
						<span class="right floated">
							<i class="money bill alternate icon"></i>
							{{ product.price }}
						</span>
					</div>
				</div>
			</div>
		</div>
		{% endfor %}
	</div>

</div>
{% endblock content %}

thanks ken solved i got the tags in Views.

Views:

def get_context_data(self, **kwargs):
        context = super(CategoryPageView, self).get_context_data(**kwargs)
        category = self.get_object()
        products = Product.objects.filter(category=category)
        tags = Product.TagsChoices
        context['tags'] = tags
        context['products'] = products
        context['page'] = f'{category.name}'
        return context

HTML

<div class="menu">
    {% for tag in tags %}
    <div class="item">
        <a href="">{{ tag.1 }}</a>
    </div>
    {% endfor %}
</div>

it works now thanks pal.