Debug School

rakesh kumar
rakesh kumar

Posted on

How to get data from another function using parameter and renders in template file in django

Update the process_students function to return a list of processed student data:

def process_students(students):
    processed_students = []
    for student in students:
        # Perform processing on each student object
        processed_student = {
            'name': student.name,
            'age': student.age,
            'grade': student.grade
        }
        processed_students.append(processed_student)
    return processed_students
Enter fullscreen mode Exit fullscreen mode

Update the main_function to pass the processed student data to the template:

from django.shortcuts import render

def main_function(request):
    students = get_students()
    processed_students = process_students(students)
    return render(request, 'template.html', {'students': processed_students})
Enter fullscreen mode Exit fullscreen mode

Create a template file called template.html in your app's template directory (e.g., myapp/templates/template.html):

<html>
<head>
    <title>Student Data</title>
</head>
<body>
    <h1>Student Data</h1>
    <ul>
    {% for student in students %}
        <li>Name: {{ student.name }}, Age: {{ student.age }}, Grade: {{ student.grade }}</li>
    {% empty %}
        <li>No students found.</li>
    {% endfor %}
    </ul>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

In this template, we use a for loop to iterate over the students list and display each student's name, age, and grade. If no students are found, it displays a message.

Now, when you access the URL associated with main_function (e.g., /my-url/), it will retrieve the student data, process it, and render the data in the template.html template.

You want to retrieve student data from the database in one function (get_students) and then pass that data to another function (process_students) for further processing. Here's a step-by-step example:

Create a Django project and app (if not already created).

Define your Student model in models.py:

from django.db import models

class Student(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField()
    grade = models.CharField(max_length=10)
Enter fullscreen mode Exit fullscreen mode

Open the views.py file in your app directory.

Define the get_students function to retrieve student data from the database:

from .models import Student

def get_students():
    students = Student.objects.all()
    return students
Enter fullscreen mode Exit fullscreen mode

Define the process_students function to process the retrieved student data:

def process_students(students):
    for student in students:
        # Perform processing on each student object
        print(f"Processing student: {student.name}, Age: {student.age}, Grade: {student.grade}")
Enter fullscreen mode Exit fullscreen mode

Define the main function, main_function, that calls both get_students and process_students:

def main_function():
    students = get_students()
    process_students(students)
Enter fullscreen mode Exit fullscreen mode

Optionally, you can pass arguments and return values between functions as needed.

Save the views.py file.

Finally, you can trigger the execution of main_function through a URL or any other entry point of your Django application.

For example, if you have a URL pattern associated with main_function in your urls.py file, accessing that URL will trigger the execution of both get_students and process_students.

from django.urls import path
from . import views

urlpatterns = [
    path('my-url/', views.main_function, name='main_function'),
]
Enter fullscreen mode Exit fullscreen mode

Accessing the URL /my-url/ will call the main_function, which, in turn, calls get_students to retrieve the student data and then passes it to process_students for further processing.

Top comments (0)