Django Frontend Data Display Guide
To display data from a Django database on the front end, usually the following steps are required:
- Create a view to retrieve data from the database.
- Use models in the view to query the database and pass the data to templates.
- Design a template file to display data retrieved from the database using Django template language.
- Display data from the database on the frontend interface using rendered templates.
Here is a simple example:
- Create a view (views.py):
from django.shortcuts import render
from .models import YourModel
def display_data(request):
data = YourModel.objects.all()
return render(request, 'your_template.html', {'data': data})
- Create a template file named “your_template.html”:
<!DOCTYPE html>
<html>
<head>
<title>Your Data</title>
</head>
<body>
<h1>Your Data:</h1>
<ul>
{% for item in data %}
<li>{{ item.field_name }}</li>
{% endfor %}
</ul>
</body>
</html>
- Display the data from the database on the frontend page using the following code:
{% extends 'base.html' %}
{% block content %}
{% include 'your_template.html' %}
{% endblock %}
In this way, the data from the Django database can be displayed on the front-end page. Please note that the above is just a simple example, and actual usage may require modifications and adjustments based on specific circumstances.