J'ai besoin de votre aide pour savoir comment je peux actualiser mon Django template
de get_context_data
.Comment actualiser le modèle Django avec get_context_data
J'ai cette classe à mon avis:
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
Et cette fonction importante dans 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
Une partie de mon modèle:
{% 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 %}
Je suis sûr que mon le modèle est chargé avant d'exécuter cette fonction et je reçois dans mon modèle NumeroIdentification = None
b ut dans ma base de données ce champ est bien rempli.
Ma question est: Comment je peux afficher ma NumeroIdentification
variable avec la bonne valeur dans mon modèle (valeur stockée dans ma base de données) au lieu de None
? Si j'appuie sur Cmd + R
(Actualiser MacOS), NumeroIdentification
ne sera pas None
mais une valeur différente. Je voudrais obtenir cette valeur dans mon modèle la première fois.
Il est assez facile avec FBV, mais avec CBV je ne surmontent pas faire
EDIT:
ajouter ma fonction 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
Merci pour votre réponse. Je comprends toutes mes choses étranges. Je dois changer 'get_context_data()' par 'post()' afin de mettre à jour mon objet et afficher cet objet mis à jour dans mon modèle? – Deadpool