How to get all data on page in Django?

Question:

How to get all data on web-page more correctly using Django?

from django.http import HttpResponse
from .models import Student


def get_students(request):
  students = Student.objects.all()
  return HttpResponse(''.join(f'<p>{student}</p>' for student in students))
Asked By: Nikolay

||

Answers:

This way you can query all the data from the model and show it on the web page.

views.py

from django.http import HttpResponse
from .models import Student  


def get_students(request):
students = Student.objects.all()
context =  {'students ': students,}
return render (request, '/index.html', context)

index.html

Now you can take all the data from the model with {{student.name}}.

 {% for student in students %}
   
  {{student.name}}

  {% endfor %}
Answered By: happycoder
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.