如何更改django变量的值

时间:2022-11-26 23:42:02

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

我需要将信息呈现为List视图,使其看起来像这样:问题2回答问题2问题1回答问题另一回答问题1

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

我正在使用Django和HTML来呈现视图,这是我目前为止的代码

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

1 个解决方案

#1


1  

You can achieve this with a for loop:

您可以使用for循环实现此目的:

{% 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

如果您希望计数器以0而不是1开头,则可以使用forloop.counter0

#1


1  

You can achieve this with a for loop:

您可以使用for循环实现此目的:

{% 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

如果您希望计数器以0而不是1开头,则可以使用forloop.counter0