Essex
Essex

Reputation: 6128

How to actualize Django Template with get_context_data

I need your help in order to know How I can actualize my Django template from get_context_data.

I have this class in my view :

class IdentitySocieteResumeView(LoginRequiredMixin,TemplateView) :

    template_name = 'Identity_Societe_Resume.html'
    model = Societe

    def get_context_data(self, **kwargs) :

        context_data = super(IdentitySocieteResumeView, self).get_context_data(**kwargs)

        id = self.kwargs['id']
        societe = get_object_or_404(Societe, pk=id)

        obj = Societe.objects.filter(Nom=societe.Nom, SIRET=societe.SIRET, SIREN=societe.SIREN, Ville=societe.Ville)

        if obj:
            sc_obj = obj[0]

            NIU = lib.Individu_Recherche.NIUGeneratorSociete(ModelBase=societe)

            societe.NumeroIdentification = NIU
            societe.save()

            context_data['queryset'] = obj

        return context_data

And this important function in lib.Individu_Recherche :

def NIUGeneratorSociete(ModelBase) :

    create_year_temp = str(ModelBase.Creation.year)
    create_year_temp2 = str(create_year_temp.split(" "))
    create_year = create_year_temp2[4] + create_year_temp2[5]

    ''' A process which let to generate NumeroIdentification '''

    NumeroIdentification = force_text('%s-%s%s-%s-%s-%s' % ('E', create_year, create_month, create_city, key, create_country_id))

    return NumeroIdentification

Part of my template :

{% block content %}

    <div class="title_subtitle_space"></div>

    <div class="resume">

    {% for societe in queryset %}

        Votre société porte le numéro : <b> {{ societe.id}} </b> <p></p>
        N° identification attribué : <b>{{societe.NumeroIdentification}}</b> <br></br>

    {% endfor %}
{% endblock %}

I'm sure my template is loaded before to execute this function and I get in my template NumeroIdentification = None but in my database this field is well-filled.

My question is : How I can display my variable NumeroIdentification with the good value in my template (value stored in my database) instead of None ?

If I press Cmd + R (MacOS Actualize), NumeroIdentification won't be None but a different value. I would like to get this value in my template the first time.

It's pretty easy with FBV, but with CBV I don't overcome to make it

EDIT :

I add my function NIUGeneratorSociete :

def NIUGeneratorSociete(ModelBase) :

    create_year_temp = str(ModelBase.Creation.year)
    create_year_temp2 = str(create_year_temp.split(" "))
    create_year = create_year_temp2[4] + create_year_temp2[5]

    create_month_temp = ModelBase.Creation.month
    if len(str(create_month_temp)) == 1 :
        create_month = '0' + str(create_month_temp)
    else :
        create_month = create_month_temp

    create_city = Villes[ModelBase.Ville]

    key_temp = randint(0,999999)
    if len(str(key_temp)) == 1 :
        key = '00000' + str(key_temp)
    elif len(str(key_temp)) == 2 :
        key = '0000' + str(key_temp)
    elif len(str(key_temp)) == 3 :
        key = '000' + str(key_temp)
    elif len(str(key_temp)) == 4 :
        key = '00' + str(key_temp)
    elif len(str(key_temp)) == 5 :
        key = '0' + str(key_temp)
    else :
        key = key_temp

    create_country = ModelBase.Pays
    create_country_id = None          
    if create_country == "CG" :
        create_country_id = 1
    else :
        create_country_id = 2

    NumeroIdentification = force_text('%s-%s%s-%s-%s-%s' % ('E', create_year, create_month, create_city, key, create_country_id))

    return NumeroIdentification

Upvotes: 1

Views: 1663

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599600

Unfortunately, this code is all kinds of confused. You're getting the same object in multiple different ways, and updating one copy but expecting the others to reflect the change.

You first get the relevant object as societe. Then for some reason you do another query on that model with all the fields of that object, to get a queryset consisting of one object. Then you do some manipulation of the original object and save it, but don't pass it to the context; instead you pass the queryset.

Your code can be simplified to just this:

def get_context_data(self, **kwargs) :

    context_data = super(IdentitySocieteResumeView, self).get_context_data(**kwargs)

    id = self.kwargs['id']
    societe = get_object_or_404(Societe, pk=id)

    NIU = lib.Individu_Recherche.NIUGeneratorSociete(societe)
    societe.NumeroIdentification = NIU
    societe.save()

    context_data['societe'] = societe

    return context_data

and the template:

{% block content %}
<div class="title_subtitle_space"></div>

    <div class="resume">

        Votre société porte le numéro : <b> {{ societe.id}} </b> <p></p>
        N° identification attribué : <b>{{societe.NumeroIdentification}}</b> <br></br>

    </div>
</div>
{% endblock %}

There are also some strange things going on in your library function. one is that you pass the object as the parameter ModelBase; although it doesn't matter what you call it, ModelBase is a class, but your parameter is an instance of your Societe class. You should call things what they are.

I can't correct that function though as it is clearly incomplete; all of create_month, create_city, key, create_country_id are undefined.

Finally, you should consider if any of this is appropriate. The update function is called from get_context_data in a normal GET request of your page; it would be very surprising for an object to be updated like this on a GET. Really this should be done on a POST only.

Upvotes: 3

Bj&#246;rn Kristinsson
Bj&#246;rn Kristinsson

Reputation: 624

Lots of weird things going on here.

After societe = get_object_or_404(Societe, pk=id) you will have a Societe instance (or 404). You then filter Societe to get a list of objects that have the same properties at the instance you already received, and then fetch the first one of those. Why not just obj = get_object_or_404(Societe, pk=id) and skip the rest?

You then mix obj, societe and sc_obj. Your actions on one of them will be lost on the others until you fetch them again, which is probably why this works on refresh. Might be helpful to see your Societe model to confirm though.

Upvotes: 1

Related Questions