【发布时间】:2017-10-18 08:11:56
【问题描述】:
我需要您的帮助才能知道如何从get_context_data 实现我的Django template。
我认为我有这门课:
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
还有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
我的模板的一部分:
{% 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 %}
我确定我的模板在执行此功能之前已加载,并且我进入了我的模板NumeroIdentification = None,但在我的数据库中,此字段已填满。
我的问题是:如何在我的模板(存储在我的数据库中的值)中显示我的变量 NumeroIdentification 而不是 None?
如果我按Cmd + R(MacOS 实现),NumeroIdentification 将不是None,而是一个不同的值。我想第一次在我的模板中获得这个值。
使用 FBV 很容易,但使用 CBV 我无法克服
编辑:
我添加我的函数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
【问题讨论】:
标签: django django-class-based-views