【问题标题】:How to actualize Django Template with get_context_data如何使用 get_context_data 实现 Django 模板
【发布时间】: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


    【解决方案1】:

    不幸的是,这段代码很混乱。您以多种不同的方式获取同一个对象,并更新一个副本,但希望其他副本反映更改。

    您首先将相关对象作为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 是一个类,但你的参数是你的 Societe 类的一个实例。你应该把事物称为它们的本来面目。

    我无法更正该功能,因为它显然不完整;所有create_month、create_city、key、create_country_id 都是未定义的。

    最后,您应该考虑其中任何一个是否合适。在您页面的正常 GET 请求中,从 get_context_data 调用更新函数;像这样在 GET 上更新对象将是非常令人惊讶的。实际上,这应该只在 POST 上完成。

    【讨论】:

    • 感谢您的回复。我明白我所有奇怪的事情。我必须将get_context_data() 更改为post() 才能更新我的对象并在我的模板中显示这个更新的对象?
    【解决方案2】:

    这里发生了很多奇怪的事情。

    在societe = get_object_or_404(Societe, pk=id) 之后,您将拥有一个 Societe 实例(或 404)。然后,您过滤 Societe 以获取在您已收到的实例中具有相同属性的对象列表,然后获取其中的第一个。为什么不只是obj = get_object_or_404(Societe, pk=id) 而跳过其余部分?

    然后混合obj、societe 和sc_obj。您对其中一个的操作将在其他操作上丢失,直到您再次获取它们,这可能是刷新时起作用的原因。不过,查看您的 Societe 模型以进行确认可能会有所帮助。

    【讨论】:

    • 你是对的。我理解您的回复,但我不知道为什么我会做出这些奇怪的台词。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    • 2020-12-14
    • 2012-11-29
    相关资源
    最近更新 更多