johnny manolly
johnny manolly

Reputation: 95

How to change the value of a django variable

So I have the following array in my source code:

values: [
        {
              "Question" :  ["question two"]   ,    "Answer" :  ["answer to question 2"]} 
         ,          {
              "Question" :  ["question one"]   ,    "Answer" :  ["answer to question one"] } 
         ,          {
              "Question" :  ["question one"]   ,    "Answer" :  ["another answer to question one"]} 

I need to render the information as a List view to make it look like this:
question two
answer to question 2
question one
answer to question one
another answer to question one

I'm using Django and HTML for rendering the view, here's my code so far

      <div>
         {% with "" as name %}  
         {% for value in view.data.values %}
         <li> 
            {% ifnotequal value.Question name %}
                <div>{{value.Question|default:''}}  {{value.question_creation_date}}</div>
             {% endifnotequal %}
                <div>{{value.user_creation_date}} {{value.Answer}}</div>
         </li>
          <!-- set name equal to value.Question -->
          {% endfor%}
          {% endwith %}
         </div>

Upvotes: 0

Views: 1067

Answers (1)

Leistungsabfall
Leistungsabfall

Reputation: 6488

You can achieve this with a for loop:

{% with "product" as name %}
    {% for i in products.count %}
        <div>{{name}}{{ forloop.counter }}</div> 
    {% endfor %}
{% endwith %}

Output:

<div>product1</div>

<div>product2</div>

etc.


If you want the counter to start with 0 instead of 1 you can use forloop.counter0

Upvotes: 1

Related Questions