2

models.py

class Skill(models.Model):

    title_choice = [
        ('LANGUAGE', 'Language'),
        ('FRAMEWORK', 'Framework')
    ]
    title = models.CharField(max_length=15, choices=title_choice,
                             default='LANGUAGE')
    name = models.CharField(max_length=50)

And the template(when compare with title) is

<h2 class="mb-5">Skills</h2>
            {% for item in Skill %}
                {% if item.title == "LANGUAGE" %}
                    <h3 class="mb-0">Programming Languages</h3>
                    <ul class="subheading mb-3">
                        <li class="list-inline-item">{{ item.name }}</li>
                    </ul>
                {% elif item.title == "FRAMEWORK" %}
                    <div class="subheading mb-3">Frameworks</div>
                    <ul class="list-inline dev-icons">
                        <li class="list-inline-item">{{ item.name }}</li>
                    </ul>

                {% endif %}
            {% endfor %}

OUTPUT: output when I compare with title

When I try to compare the title from my model it doesn't show any data But when I compare the "name" with the database value it shows the correct output. template(when I compare with name)

<h2 class="mb-5">Skills</h2>
            {% for item in Skill %}
                {% if item.name == "Python" %}
                    <h3 class="mb-0">Programming Languages</h3>
                    <ul class="subheading mb-3">
                        <li class="list-inline-item">{{ item.name }}</li>
                    </ul>
                {% elif item.name == "React" %}
                    <h3 class="mb-0">Frameworks</h3>
                    <ul class="subheading mb-3">
                        <li class="list-inline-item">{{ item.name }}</li>
                    </ul>

                {% endif %}
            {% endfor %}

OUTPUT:when I compare with name

I want to compare the value with title and show the results

4
  • whar's the value of {{item.title}} ?
    – arjun
    Aug 7, 2020 at 6:33
  • value is either "LANGUAGE" or "FRAMEWORK" Aug 7, 2020 at 6:35
  • Is the value stored in the db also ? Did you check that ?
    – arjun
    Aug 7, 2020 at 6:36
  • Yes they're store with a choice filed. Aug 7, 2020 at 6:40

1 Answer 1

0

In your template, you want to be able to compare the Charfield against a CONSTANT in class / instance. To do that, you'll have to modify your code as below:

class Skill(models.Model):
    LANGUAGE = 'L'
    FRAMEWORK = 'F'

    title_choice = [
        (LANGUAGE, 'Language'),
        (FRAMEWORK, 'Framework')
    ]
    title = models.CharField(max_length=15, choices=title_choice,
                         default='LANGUAGE')
    name = models.CharField(max_length=50)

The value you define against the CONSTANT will be stored in the db i.e. 'L' for Language.

The second value in the tuple is what django will return when you call the get_title_display() function.

In your template you can now do:

{% if item.title == item.LANGUAGE %}

to compare against the choices.

https://docs.djangoproject.com/en/3.1/ref/models/fields/#choices

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.