简体   繁体   English

基于类的视图上有多个Django函数

[英]Multiple Django functions on class-based view

I'm looking for using Multiple Django functions in the same Class and display these functions on the same template. 我正在寻找在同一类中使用Multiple Django functions ,并在同一模板上显示这些函数。

Both functions are in my Django Template, but none work and I don't know why. 这两个函数都在我的Django模板中,但是没有任何作用,我也不知道为什么。

First function : 第一个功能:

This function lets to search a person in my Database based on 3 criteria : firstname , lastname and birthcity . 该功能可让搜索基于三个标准在我的数据库中的一个人: firstnamelastnamebirthcity Then I display the query result in my array. 然后,我在数组中显示查询结果。

Second function : 第二功能:

This function is a simple Django form . 此函数是一个简单的Django form

This is my first Django Class : 这是我的第一个Django类别:

class SocieteFormulaire(TemplateView) :

    template_name= "Societe.html"
    model = Societe


    def get_context_data(self, **kwargs):

        request = self.request

        if 'recherche' in request.GET:

            query_Lastname_ID = request.GET.get('q1LastnameID')
            query_Firstname_ID = request.GET.get('q1FirstnameID')
            query_BirthCity_ID = request.GET.get('q1BirthCityID')

            sort_params = {}

            set_if_not_none(sort_params, 'Lastname__icontains', query_Lastname_ID)
            set_if_not_none(sort_params, 'Firstname__icontains', query_Firstname_ID)
            set_if_not_none(sort_params, 'BirthCity__icontains', query_BirthCity_ID)

            query_list = Societe_Recherche.Recherche_Filter(Societe, sort_params)

            context = {
                "query_Lastname_ID" : query_Lastname_ID,
                "query_Firstname_ID" : query_Firstname_ID,
                "query_BirthCityID" : query_Birthcity_ID,
                "query_list" : query_list,
            }

            return context


    def get_context_data(self, **kwarg) :

        success = False
        request = self.request

        if request.method == 'POST':

            form = IndividuFormulaire(request.POST or None, request.FILES or None)

            if form.is_valid() :
                post = form.save(commit=False)
                for element in settings.BDD :
                    post.save(using=element, force_insert=True)

                messages.success(request, 'Le formulaire a été enregistré !')
                return HttpResponseRedirect(reverse('IndividuResume', kwargs={'id': post.id}))

            else:
                messages.error(request, "Le formulaire est invalide !")

        else:
            form = IndividuFormulaire()
            form.fields['Utilisateur'].initial = request.user.last_name + " " + request.user.first_name

        context = {
            "form" : form,
            "Individu" : Individu
        }

        return context

I named both functions with the same name, because if I don't do that, first function or my form is not displayed on my template. 我用相同的名称命名这两个函数,因为如果不这样做,则第一个函数或我的表单不会显示在模板上。

So my question is : How I can set multiple functions in the same Django Class and in the same Django template ? 所以我的问题是:如何在同一Django类和同一Django模板中设置多个功能?

You have several misconceptions and misunderstandings here. 您在这里有一些误解和误解。

You simply can't have two methods with the same name in a Python class. 在Python类中,您根本不能拥有两个具有相同名称的方法。 The first one will just be overridden when the class is loaded. 加载类时,第一个将被覆盖。 The normal solution is to put all the code in one method, or have two with different names and call one from the other. 正常的解决方案是将所有代码放在一个方法中,或者使用两个具有不同名称的代码,然后从另一个方法中调用一个。

However, you should also consider that none of this logic belongs in get_context_data anyway. 但是,您还应该考虑到,无论如何,此逻辑都不属于get_context_data That's for preparing context data for the template, not for processing form submissions. 那是为了为模板准备上下文数据,而不是用于处理表单提交。

The fundamental problem is that you are picking the wrong class to inherit and then overriding the wrong methods. 根本的问题是您选择了错误的类来继承,然后覆盖了错误的方法。 TemplateView is a very basic class and is certainly not meant to be used like this. TemplateView是一个非常基本的类,当然不打算这样使用。 The real action of your view is to display and process a form that creates an element; 视图的真正作用是显示和处理创建元素的表单。 so, you should use CreateView. 因此,您应该使用CreateView。 Then you can get rid of almost all the code in your second method; 然后,您可以摆脱第二种方法中的几乎所有代码; the view will do all the processing for you, the only thing you will need to do is to define form_valid to do your custom save logic. 该视图将为您完成所有处理,您唯一需要做的就是定义form_valid来执行自定义保存逻辑。

The search logic could remain in get_context_data if you like. 如果您愿意,搜索逻辑可以保留在get_context_data But you do need to remember to call the superclass method, and also return context even if 'recherche' in request.GET is false. 但是,您确实需要记住调用超类方法,并且即使'recherche' in request.GET为false,也要返回上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM