python - 如何更改 Django 中模型(类)的 HTML?

标签 python html django django-models django-templates

假设我在 myproject/myapp/models.py 中有一个 TrueFalseQuestion 模型(只有一个 question_text),并且我想要它比方说,被渲染为

<a>Question Text</a>
<input type="radio">True</input>
<input type="radio">False</input>

(HTML可能是错误的,并不重要),在这个模板中,只有Question Text部分发生变化,输入形式始终不变。那么如何在 Django 中实现这种行为呢?

举个例子,我们将不胜感激。谢谢!

最佳答案

在应用程序的views.py 文件中,添加以下内容。

from django.views.generic import ListView
from models import TrueFalseQuestion

class QuestionsList(ListView):
    queryset = TrueFalseQuestion.objects.all()
    template_name = 'name_of_your_template'

然后在模板内,像这样迭代 object_list:

{%for ques in object_list%}
<a>{{ques.question_text}}</a>
<input type="radio">True</input>
<input type="radio">False</input>
{%endfor%}

此外,请确保将您的网址指向该 View 。

关于python - 如何更改 Django 中模型(类)的 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28828721/

相关文章:

javascript - 猫头鹰轮播定位div

python - 检测 numpy-3D 数组中的极端(最远)点

python - 使用列表理解表达式用对象填充列表

python - 如何使基于 `unicode`的逻辑兼容python2-3

html - CSS 边框属性

javascript - 如何验证按钮是否被点击?

django - Django日志: Change sender email address from root@localhost to something else

python - 如何编写从 zip 文件创建 InMemoryUploadedFile 的 python 测试?

python - Django self.cleaned_data 不工作

python - 使用 pandas groupby 创建 df 的子集并根据函数获取值