Home > OS >  How to get all data on page? Django
How to get all data on page? Django

Time:07-29

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))

CodePudding user response:

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