# Access Model CharField() Choices in HTML

**URL:** <https://forum.djangoproject.com/t/access-model-charfield-choices-in-html/14538>\
**Category:** Templates & Frontend\
**Created:** [June 29, 2022, 12:55pm UTC](https://forum.djangoproject.com/t/access-model-charfield-choices-in-html/14538 "2022-06-29T12:55:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AhmdMWddh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ahmdmwddh/32/21707_2.png) [@AhmdMWddh](https://forum.djangoproject.com/u/AhmdMWddh)\
**Post date:** [June 29, 2022, 12:55pm UTC](https://forum.djangoproject.com/t/access-model-charfield-choices-in-html/14538/1 "2022-06-29T12:55:11Z")

</div>

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**

```auto
<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:**

```auto
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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 30, 2022, 2:58pm UTC](https://forum.djangoproject.com/t/access-model-charfield-choices-in-html/14538/2 "2022-06-30T14:58:07Z")

</div>

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')`

---

<div class="post-metadata">

**Author:** ![AhmdMWddh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ahmdmwddh/32/21707_2.png) [@AhmdMWddh](https://forum.djangoproject.com/u/AhmdMWddh)\
**Post date:** [June 30, 2022, 4:01pm UTC](https://forum.djangoproject.com/t/access-model-charfield-choices-in-html/14538/3 "2022-06-30T16:01:49Z")

</div>

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**

```auto
<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 %}

```

---

<div class="post-metadata">

**Author:** ![AhmdMWddh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ahmdmwddh/32/21707_2.png) [@AhmdMWddh](https://forum.djangoproject.com/u/AhmdMWddh)\
**Post date:** [June 30, 2022, 4:20pm UTC](https://forum.djangoproject.com/t/access-model-charfield-choices-in-html/14538/4 "2022-06-30T16:20:34Z")

</div>

thanks ken solved i got the tags in Views.

**Views:**

```auto
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**

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

```

it works now thanks pal.
