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