-1

I have the following Model in django.

class Choices(models.Model):
    question = models.CharField(max_length=200)
    option1 = models.CharField(max_length=200)
    option2 = models.CharField(max_length=200)
    option3 = models.CharField(max_length=200)

I would like to display in my template the element of option2 field with id=1. Is there a way I can do this?

Thank you.

Tim
  • 47
  • 2
  • 8
  • possible duplicate of http://stackoverflow.com/questions/4300365/django-database-query-how-to-get-object-by-id – marcusshep Jul 05 '16 at 17:55

1 Answers1

0

You should get the Choice inside your view and then pass it to your template. Example:

view.py

from django.shortcuts import get_object_or_404, render

def my_view(request, choice_id):
    choice = get_object_or_404(Choice, id=choice_id)
    return render(request, "my_template.html", {"choice": choice}

my_template.html

<html>
  <body>
    {{ choice.option2 }}
  </body>
</html>
Mary Marchini
  • 718
  • 5
  • 12