'The data/content isn't getting displayed. Can anyone please let me know any necessary changes to render the data?

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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sc?riolus