0

I have a Question model that I am passing as a context to the template

class Question(models.Model):
    possible_answer1 = models.TextField(
        'Possible Answer 1',
        max_length=1000,
        default='none'
    )
    possible_answer2 = models.TextField(
        'Possible Answer 2',
        max_length=1000,
        default='none',
        blank=True
    )
    possible_answer3 = models.TextField(
        'Possible Answer 3',
        max_length=1000,
        default='none',
        blank=True
    )
    possible_answer4 = models.TextField(
        'Possible Answer 4',
        max_length=1000,
        default='none',
        blank=True
    )
    possible_answer5 = models.TextField(
        'Possible Answer 5',
        max_length=1000,
        default='none',
        blank=True
    )
    possible_answer6 = models.TextField(
        'Possible Answer 6',
        max_length=1000,
        default='none',
        blank=True
    )

I can access all the answer object no problem if I use

{{ context.this_question.question.possible_answer1 }}
{{ context.this_question.question.possible_answer2 }}
.
.
{{ context.this_question.question.possible_answer6 }}

The problem is that I don't need all 6 answers to be displayed all the time so I was trying to build for loop that would only dispay what I need at a certain time based on the following thread using the with tag How to concatenate strings in django templates?1 . In the example below mylist contain four elements so I tried this

{% for x in mylist %}
    {% with possible_answer='context.this_question.question.possible_answer'|add:forloop.counter %}
      {{ possible_answer }}
    {% endwith %}
{% endfor %}

In the hope that it would generate

{{ context.this_question.question.possible_answer1 }}
{{ context.this_question.question.possible_answer2 }}
{{ context.this_question.question.possible_answer3 }}
{{ context.this_question.question.possible_answer4 }}

and consequently display the corresponding item from the context object as if I was typing it myself but it doesn't work. Nothing is displaying

If replace forloop.counter with any string it would then display the concatened string without trying fetch the value in the context.

Anyone has an idea on how to achieve this?

Fabou78
  • 169
  • 1
  • 2
  • 12

1 Answers1

0

That kind of logic is better generated at the view. You may as well, will need to rethink your database architecture. When I am presented that kind of problems I preferably create two diferent models.

For instance a Question model and an Answer model. The Answer model would have a Foreign Key to the Question model and then with a simple filter get all the answers related to that question. It is a simple programming paradigm of SQL databases.

models.py

class Question(models.Model):
  Body = models.TextField(max_length=1000,default='none',blank=True)

class Answer(models.Model):
  Question = models.ForeignKey(Question)
  Body = models.TextField(max_length=1000,default='none',blank=True)

views.py:

Q = Question.objects.get(pk='id')
A = Answer.objects.filter(Question=Q)

I hope it helps!

Santiago M. Quintero
  • 1,237
  • 15
  • 22
  • 1
    Didn't think about separating question and answer. Will think about it thanks. Still I would like to know if what I am trying to achieve is possible at all directly from template. – Fabou78 Dec 08 '17 at 19:19