[Django]-Django Query set to fetch a data from a database?

6👍

I tried to understand your question and I will answer with an example from my Django web application.

In my case, but yours is similar, I have a template which renders all companies from my database in a table. I display all companies and if I click in a cell, I can consult a new template with all informations according to this company. It’s exactly the same issue that you asking for.

First step : urls.py file

In this file, I have 3 urls according to your issue :

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^Formulaire/Societes$', views.Identity_Societe_Form, name = "SocieteFormulaire"),
    url(r'^Resume/Societes$', views.Identity_Societe_Resume, name = "SocieteResume"),
    url(r'^Contrat/Societe/(?P<id>\d+)/$', views.Identity_Contrat, name="Contrat"),
]
  • SocieteFormulaire lets to fill the form and save the object in my database
  • SocieteResume lets to display all companies in a table with multiple informations. In this table, I can click inside in order to display the company template.
  • Contrat lets to display a template in function of the choosen company

Second step : view.py file with Resume function

In my view, I have a function which let to display all companies inside an HTML Template.

@login_required
def Identity_Societe_Resume(request) :

    societe = Societe.objects.all()
    contrat = SocieteContrat.objects.all()

    paginator = Paginator(societe, 10)
    page = request.GET.get('page', 1)

    try:
        societe = paginator.page(page)
    except PageNotAnInteger:
        societe = paginator.page(1)
    except EmptyPage:
        societe = paginator.page(paginator.num_pages)

    paginator = Paginator(contrat, 10)
    page = request.GET.get('page', 1)

    try:
        contrat = paginator.page(page)
    except PageNotAnInteger:
        contrat = paginator.page(1)
    except EmptyPage:
        contrat = paginator.page(paginator.num_pages)

    context={
        "societe" : societe,
        "PageNotAnInteger":PageNotAnInteger,
        "contrat" : contrat,
    }

    return render(request, 'Identity_Societe_Resume.html', context) 

I display all companies registered in my database in this html template named : Identity_Societe_Resume.html

The core of this template looks like :

<h4><b> <span class="glyphicon glyphicon-user"></span></span> Récapitulatif des Sociétés ayant souscrit à un contrat de services : </b></h4>
            <table style="width:125%">
                <tbody>
                <tr>
                    <th>ID</th>
                    <th>Nom</th>
                    <th>État</th>
                    <th>SIRET</th>
                    <th>SIREN</th>
                    <th>NAF-APE</th>
                    <th>Adresse</th>
                    <th>Ville</th>
                    <th>Pays</th>
                </tr>
                {% for item in societe %}
                <tr>
                    <td><a href="http://localhost:8000/Identity/Contrat/Societe/{{item.id}}"> Ici </a></td>
                    <td>{{ item.Nom}}</td>
                    <td>{{ item.Etat}}</td>
                    <td>{{ item.SIRET }}</td>
                    <td>{{ item.SIREN }}</td>
                    <td>{{ item.NAF_APE }}</td>
                    <td>{{ item.Adresse }}</td>
                    <td>{{ item.Ville}}</td>
                    <td>{{ item.Pays.name }}</td>
                </tr>
                {% endfor %}
                </tbody>
            </table>

As you can see, my table displays some informations and displays the company ID. But this information is a link to the company template according to the urls.py file shown previously.

Third step : views.py file with company informations

In this step, I display informations according to the good company.
In my url, I have : http://localhost:8000/Identity.Contrat/Societe/1

Number 1 displays informations about company 1

Then I have in my view :

@login_required
def Identity_Contrat(request, id) :

        societe = get_object_or_404(Societe, pk=id)
        contrat = get_object_or_404(SocieteContrat, pk=id)
        #etc ...

It’s really important to have : (request, id) and in each queryset I referred about company ID

Finally, in my template I have :

<h4><b> Récapitulatif concernant la société : {{societe.Nom}}</b></h4>
            <table style="width:125%">
                <tbody>
                <tr>
                    <th>ID</th>
                    <th>Nom</th>
                    <th>État</th>
                    <th>SIRET</th>
                    <th>SIREN</th>
                    <th>NAF-APE</th>
                    <th>Adresse</th>
                    <th>Ville</th>
                    <th>Pays</th>
                </tr>
                <tr>
                    <td>{{societe.id}}</td>
                    <td>{{ societe.Nom}}</td>
                    <td>{{ societe.Etat}}</td>
                    <td>{{ societe.SIRET }}</td>
                    <td>{{ societe.SIREN }}</td>
                    <td>{{ societe.NAF_APE }}</td>
                    <td>{{ societe.Adresse }}</td>
                    <td>{{ societe.Ville}}</td>
                    <td>{{ societe.Pays.name }}</td>
                </tr>
                </tbody>
            </table>

Hopefully this example is helpful and I apologize about my English which is very suck ..

👤Essex

Leave a comment