You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法显示表格内容:Question_page.html仅展示表头不加载模型数据原因排查

Troubleshooting: Django Table Not Loading Model Data

Hey there, let’s walk through the most common reasons your Question_page.html is only showing the "Questions" header and no actual data from your Question model. I’ve hit these exact snags before, so let’s break them down:

1. Your View Isn’t Passing Data to the Template

If your views.py isn’t fetching the question data or sending it to the template via context, the template has nothing to display.

For function-based views, make sure you’re querying the model and including the data in the render() call:

# views.py example
from django.shortcuts import render
from .models import Question

def question_page(request):
    # Fetch all questions (adjust filtering as needed)
    questions = Question.objects.all()
    # Pass the queryset to the template with a context key
    return render(request, 'Question_page.html', {'questions': questions})

For class-based views (like ListView), double-check you’ve set the model/queryset and context name:

# views.py class-based example
from django.views.generic import ListView
from .models import Question

class QuestionPageView(ListView):
    model = Question
    template_name = 'Question_page.html'
    context_object_name = 'questions'  # Matches the variable your template uses

2. Template is Using the Wrong Variable Name (or Syntax Errors)

Even if the view sends data, if your template loops over the wrong variable or has syntax mistakes, nothing will render.

First, confirm your template is looping over the same context key from the view. For example:

<!-- Question_page.html example -->
<table>
    <thead>
        <tr>
            <th>Questions</th>
        </tr>
    </thead>
    <tbody>
        <!-- Loop over the 'questions' variable from your view -->
        {% for question in questions %}
            <tr>
                <!-- Make sure this matches your model's field name (e.g., question_text, content) -->
                <td>{{ question.question_text }}</td>
            </tr>
        {% empty %}
            <!-- Add this to debug if the queryset is empty -->
            <tr>
                <td>No questions found in the database.</td>
            </tr>
        {% endfor %}
    </tbody>
</table>

Common mistakes here: typos in the variable name (e.g., question instead of questions), missing {% endfor %}, or referencing a model field that doesn’t exist.

3. There’s No Data in Your Database

Sometimes the code is right, but your Question table is empty! Verify this via the Django shell:

  1. Run python manage.py shell in your terminal
  2. Import your model: from your_app_name.models import Question
  3. Check for data: print(Question.objects.all())

If this returns an empty queryset (<QuerySet []>), you need to add test data—either via the Django admin, a fixture, or manually creating objects in the shell.

4. Queryset Filtering is Too Strict

If your view uses a filter (e.g., Question.objects.filter(is_published=True)), make sure some questions in the database match that condition. For example, if you’re filtering for published questions but none are marked as published, the queryset will be empty.

5. Debug Mode is Off (Hiding Errors)

If DEBUG = False in your settings.py, Django won’t show detailed error messages that could point to the issue. Flip it to True temporarily—you might see a template syntax error, missing model field, or view issue that’s been hidden.

Hope one of these fixes gets your table loading data again! Feel free to share snippets of your code if you need further help narrowing it down.

内容的提问来源于stack exchange,提问作者Shri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:38:29