【问题标题】:how to assign a new value to a variable in django如何为django中的变量分配新值
【发布时间】:2015-08-17 13:21:07
【问题描述】:

我的源代码中有以下数组:

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"]} 

我需要将信息呈现为ListView 使其看起来像这样:

问题二
回答问题 2
问题一
回答问题一
第一个问题的另一个答案

我正在使用 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>

如何显示ListView

【问题讨论】:

  • 你的问题在哪里?
  • 我想知道如何像上面的示例一样将信息显示为列表视图

标签: python django html django-templates django-views


【解决方案1】:

Python 没有数组 Python有列表、字典、元组 现在看看你的代码,你有这个: 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"]}
这不正确! 您在代码末尾错过了 ]!

再看一遍你说的 ==> 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"]}

这是一个 dict 列表,因为你有 value=[dict_0, dict_1, ...]

现在假设您将它返回到视图中!要渲染它,您可以执行以下操作:
{% for v in values %}: <div class="question_answer"> <p class="line_question_answer"> <span class="question"> {% trans "question"%}: {{v.Question}} </span> <span class="answer"> {% trans "Answer" %}: {{v.Answer}} </span> </p> </div> {% endfor %}

【讨论】:

    【解决方案2】:

    你已经为它构建了数据结构。并根据它在模板上渲染它。

    例如

    查看代码,

    context = {}
    values = [
    
    {'question':'A','answer':'a'},
    {'question':'B','answer':'b'},
    {'question':'C','answer':'c'},
    {'question':'D','answer':'d'},
    
    ]
    context['values'] = values
    

    模板代码,

    {% for i in values %}
    <p>
    {{i.question}}
    </br>
    {{i.answer}}
    </p>
    {% endfor %}
    

    【讨论】:

      【解决方案3】:

      ListView 呈现数据库中的对象列表。因此,您需要有两个模型:一个用于Question,一个用于Answer(注意使用单数)。

      from django.db import models
      
      class Question(models.Model):
          text = models.CharField(max_length=250)
      
      class Answer(models.Model):
          question = models.OneToOneField(question)
          text = models.CharField(max_length=250)
      

      现在您已经有了模型,我们可以使用ListView 来渲染视图,如下所示:

      from django.views.generic import ListView
      from . import models
      
      class MyView(ListView):
          model = Question
      

      然后在您应用的以下文件夹中创建一个模板:templates/appname/templates/question_list.html,其中包含以下内容:

      <!DOCTYPE html>
      <html>
      <body>
        <p>
        {% for question in object_list %}
            {{ question.text }}<br>
            {{ question.answer.text }}
        {% empty %}
          No questions found.
        {% endfor %}
        </p>
      </body>
      </html>
      

      最后,在 urls.py 主文件中包含一个 URLconf 并使用以下行:

      from appname.views import MyView
      ...
      url(r'^myapp/$', MyView.as_view()),
      ...
      

      这应该让你开始。 谁有更正要补充的?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-24
        相关资源
        最近更新 更多