数据/内容不显示显示。谁能让我知道任何必要的更改以渲染数据?
models.py
class Question(models.Model):
# id = models.AutoField(primary_key=True)
question=models.CharField(max_length=600)
option1=models.CharField(max_length=200, default=None)
option2=models.CharField(max_length=200, default=None)
option3=models.CharField(max_length=200, default=None)
option4=models.CharField(max_length=200, default=None)
views.py
def Practice_all(request):
practice = Question.objects.all()
context={ 'question_list': practice }
return render(request, 'practice.html', context)
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("practice/", views.Practice_all, name="practice"),
]
练习
{% extends 'base.html' %}
{% block content %}
{% for data in question_list %}
<li> {{ data.option1 }} </li>
<li> {{ data.option2 }} </li>
<li> {{ data.option3 }} </li>
{% endfor %}
{% endblock %}
。有什么建议吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调试的一些提示:
问题
表不是空的,并且问题确实来自这里?检查使用python manage.py shell
在shell中(或使用诸如dbeaver之类的数据库可视化器),{{Question_list}}
在行下{%block content;
data
进行迭代QUACTION_LIST
,我想知道您页面的上下文
在您的页面的上下文
非常通用的关键字。选择更明确的东西,例如“问题”,它将更清楚并降低冲突的风险:A few tips for debugging:
Question
table in your database is not empty and that the problem really stems from here? Check that usingpython manage.py shell
and by runningQuestion.objects.all()
in the shell (or use a database visualizer like DBeaver),{{ question_list }}
under the line{% block content %}
, outside of thefor
loop in your HTML file: does it display anything?data
to iteratequestion_list
, I wonder if this variable name could not already be used in thecontext
of your page since it's a very generic keyword. Choose something more explicit like "question", it will be clearer and reduce the risk of a conflict: