如何为django中的变量分配新值
how to assign a new value to a variable in django
我的源代码中有以下数组:
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
以使其看起来像这样:
question two
answer to question 2
question one
answer to question one
another answer to question one
我正在使用 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
?
您已经为它建立了数据结构。并根据它在模板上渲染它。
例如
查看代码,
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 %}
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"]}
这是一个字典列表,因为你有 value=[dict_0, dict_1, ...]
现在假设您在视图中重新返回它!要渲染它,您可以这样做:
<code>{% 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 %}
A 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()),
...
这应该可以帮助您入门。
有人要补充更正吗?
我的源代码中有以下数组:
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
以使其看起来像这样:
question two
answer to question 2
question one
answer to question one
another answer to question one
我正在使用 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
?
您已经为它建立了数据结构。并根据它在模板上渲染它。
例如
查看代码,
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 %}
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"]}
这是一个字典列表,因为你有 value=[dict_0, dict_1, ...]
现在假设您在视图中重新返回它!要渲染它,您可以这样做:
<code>{% 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 %}
A 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()),
...
这应该可以帮助您入门。 有人要补充更正吗?