Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django: Display data from other table

I’m having a hard time displaying some datas from two or three tables. So this is my code for showing all the data only from Device table.

def device(request):
    if request.method == 'GET':
        queryset = Device.objects.all()
        if queryset:
            data = {"queryset": queryset}
            return render(request, 'device/device.html', data)
        else:
            return render(request, 'device/device.html')

And I want to add some data from other table, how do I insert it here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just make the queries and put the results in the render context, and refer to those in your template.

    return render(request, 'device/device.html', {
        "devices": Device.objects.all(),
        "thingabobs": Thingabob.objects.all(),
        "chili_pickles": Pickle.objects.filter(flavor="chili"),
        "plain_pickles": Pickle.objects.filter(flavor="plain"),
    })
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading