如何为django中的变量赋值新值

时间:2015-08-17 13:21:08

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

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

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

3 个答案:

答案 0 :(得分:1)

您已为其构建数据结构。并根据它在模板上呈现它。

例如。

查看代码,

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

答案 1 :(得分:1)

Python没有数组 Python有list,dict,tuple 现在看看你的代码: 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"]}
这不正确! 您错过了]代码结束了!

再看看你说再次==&gt; 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列表,因为你有值= [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 :(得分:1)

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>

最后,在主URLconf文件中加入urls.py,其中包含以下行:

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

这应该让你开始。 有任何更正要添加的人吗?