数据/内容不显示显示。谁能让我知道任何必要的更改以渲染数据?

发布于 2025-01-30 11:55:54 字数 1124 浏览 1 评论 0 原文

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

。有什么建议吗?

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"),
    
]

practice.html

{% extends 'base.html' %}
{% block content %} 
  {% for data in question_list %} 
    <li> {{ data.option1 }} </li>
    <li> {{ data.option2 }} </li>
    <li> {{ data.option3 }} </li>
  {% endfor %} 
{% endblock %}

These are the Django files that I am using, the server runs perfectly but won't display anything, not even any errors, just blank. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜味超标? 2025-02-06 11:55:54

调试的一些提示:

  • 您确定数据库中的问题表不是空的,并且问题确实来自这里?检查使用 python manage.py shell 在shell中(或使用诸如dbeaver之类的数据库可视化器),
  • 尝试可视化 {{Question_list}} 在行下{%block content;
  • 避免使用关键字 data 进行迭代 QUACTION_LIST ,我想知道您页面的上下文在您的页面的上下文非常通用的关键字。选择更明确的东西,例如“问题”,它将更清楚并降低冲突的风险:
{% for question in question_list %} 
     ...

A few tips for debugging:

  • Are you sure that the Question table in your database is not empty and that the problem really stems from here? Check that using python manage.py shell and by running Question.objects.all() in the shell (or use a database visualizer like DBeaver),
  • Try to visualize {{ question_list }} under the line {% block content %}, outside of the for loop in your HTML file: does it display anything?
  • Avoid using the keyword data to iterate question_list, I wonder if this variable name could not already be used in the context 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:
{% for question in question_list %} 
     ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文