如何使用 get_context_data 实现 Django 模板
How to actualize Django Template with get_context_data
我需要你的帮助才能知道如何从 get_context_data
实现我的 Django template
。
我认为 class :
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 Actualize),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
这里发生了很多奇怪的事情。
在 societe = get_object_or_404(Societe, pk=id)
之后您将拥有一个 Societe 实例(或 404)。然后,您过滤 Societe 以获取在您已收到的实例中具有相同属性的对象列表,然后获取其中的第一个。为什么不只是 obj = get_object_or_404(Societe, pk=id)
而跳过其余的呢?
然后混合 obj
、societe
和 sc_obj
。在您再次获取它们之前,您对其中一个的操作将对其他操作丢失,这可能就是刷新时起作用的原因。不过,查看您的 Societe
模型以进行确认可能会有所帮助。
不幸的是,这段代码很混乱。您正在以多种不同的方式获取同一个对象,并更新一个副本但期望其他副本反映更改。
你首先得到相关对象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)
NIU = lib.Individu_Recherche.NIUGeneratorSociete(societe)
societe.NumeroIdentification = NIU
societe.save()
context_data['societe'] = societe
return context_data
和模板:
{% 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 %}
您的库函数中也发生了一些奇怪的事情。一种是您将对象作为参数 ModelBase 传递;虽然不管你怎么称呼它,ModelBase是一个class,但是你的参数是你的Societeclass的一个实例。你应该称呼事物本来的样子。
我无法更正该功能,因为它显然不完整; create_month
、create_city
、key
、create_country_id
均未定义。
最后,你应该考虑这是否合适。在您页面的正常 GET 请求中从 get_context_data
调用更新函数;在 GET 上像这样更新对象将是非常令人惊讶的。实际上,这应该只在 POST 上完成。
我需要你的帮助才能知道如何从 get_context_data
实现我的 Django template
。
我认为 class :
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 Actualize),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
这里发生了很多奇怪的事情。
在 societe = get_object_or_404(Societe, pk=id)
之后您将拥有一个 Societe 实例(或 404)。然后,您过滤 Societe 以获取在您已收到的实例中具有相同属性的对象列表,然后获取其中的第一个。为什么不只是 obj = get_object_or_404(Societe, pk=id)
而跳过其余的呢?
然后混合 obj
、societe
和 sc_obj
。在您再次获取它们之前,您对其中一个的操作将对其他操作丢失,这可能就是刷新时起作用的原因。不过,查看您的 Societe
模型以进行确认可能会有所帮助。
不幸的是,这段代码很混乱。您正在以多种不同的方式获取同一个对象,并更新一个副本但期望其他副本反映更改。
你首先得到相关对象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)
NIU = lib.Individu_Recherche.NIUGeneratorSociete(societe)
societe.NumeroIdentification = NIU
societe.save()
context_data['societe'] = societe
return context_data
和模板:
{% 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 %}
您的库函数中也发生了一些奇怪的事情。一种是您将对象作为参数 ModelBase 传递;虽然不管你怎么称呼它,ModelBase是一个class,但是你的参数是你的Societeclass的一个实例。你应该称呼事物本来的样子。
我无法更正该功能,因为它显然不完整; create_month
、create_city
、key
、create_country_id
均未定义。
最后,你应该考虑这是否合适。在您页面的正常 GET 请求中从 get_context_data
调用更新函数;在 GET 上像这样更新对象将是非常令人惊讶的。实际上,这应该只在 POST 上完成。