Essex
Essex

Reputation: 6118

Migrate Function Based Views to Class Based views

According to this post, I'm trying to modify my whole script in order to get Class Based Views (CBV) in my Django application.

I would like to get any help, because it's the first time I'm using CBV.

My previous script function looks like this :

@login_required
def IdentityIndividuForm(request) :

    success = False
    query_Nom_ID = query_Prenom_ID = query_VilleNaissance_ID = None

    if 'recherche' in request.GET:

        query_Nom_ID = request.GET.get('q1NomID')
        query_Prenom_ID = request.GET.get('q1PrenomID')
        query_VilleNaissance_ID = request.GET.get('q1VilleNaissanceID')

        sort_params = {}

        lib.Individu_Recherche.set_if_not_none(sort_params, 'Nom__icontains', query_Nom_ID)
        lib.Individu_Recherche.set_if_not_none(sort_params, 'Prenom__icontains', query_Prenom_ID)
        lib.Individu_Recherche.set_if_not_none(sort_params, 'VilleNaissance__icontains', query_VilleNaissance_ID)

        query_ID_list = Individu.objects.filter(**sort_params) 

    else :
        query_ID_list = Individu.objects.none()

    if request.method == 'POST':

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

        if form.is_valid() :
            post = form.save()

            return HttpResponseRedirect(reverse('IndividuResume', kwargs={'id': post.id}))

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

    context = {
        "form" : form,
        "Individu" : Individu,
        "query_Nom_ID" : query_Nom_ID,
        "query_Prenom_ID" : query_Prenom_ID,
        "query_VilleNaissance_ID" : query_VilleNaissance_ID,
        "query_ID_list" : query_ID_list,
    }

    return render(request, 'Identity_Individu_Form.html', context)

I had a GET part and a POST part in my function but both part are independent. The first one lets to make a research over my database. The second one lets to create an object to my database.

My question is : How I can overwrite the GET part with function based on CBV ?

My new function form_valid(self, form) works well, but I don't overcome to migrate the GET part to my CBV part.

Thank you!

Upvotes: 0

Views: 164

Answers (2)

user8060120
user8060120

Reputation:

You can try it, first get super context data, after it do processing as you wish and after it update the contest data with yours

by default CreateView is subclass of the ProcessFormView that provide get method, as:

def get(self, request, *args, **kwargs):
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    return self.render_to_response(self.get_context_data(form=form))

so you can try to override the get method with return as:

    return self.render_to_response(self.get_context_data(request, form=form))

and the get_context_data

def get_context_data(self, **kwargs) :

    context_data = super(IdentityIndividuFormView, self).get_context_data(**kwargs)

    if 'recherche' in self.request.GET:

        query_Nom_ID = self.request.GET.get('q1NomID')
        query_Prenom_ID = self.request.GET.get('q1PrenomID')
        query_VilleNaissance_ID = self.request.GET.get('q1VilleNaissanceID')

        sort_params = {}

        lib.Individu_Recherche.set_if_not_none(sort_params, 'Nom__icontains', query_Nom_ID)
        lib.Individu_Recherche.set_if_not_none(sort_params, 'Prenom__icontains', query_Prenom_ID)
        lib.Individu_Recherche.set_if_not_none(sort_params, 'VilleNaissance__icontains', query_VilleNaissance_ID)

        query_ID_list = Individu.objects.filter(**sort_params)
        context_data['queryset'] = query_ID_list

    else :
        query_ID_list = Individu.objects.none()
        context_data['queryset'] = query_ID_list

    return context_data

Upvotes: 1

Abhishek Agarwala
Abhishek Agarwala

Reputation: 754

I see another issue here, that you missed out @login_required part of the check here, to do so use the mixin like this :

from django.contrib.auth.mixins import LoginRequiredMixin

class IdentityIndividuFormView(LoginRequiredMixin, CreateView) :

And I suppose in case of GET request you can update the context using this:

def get(self, request, *args, **kwargs):

Upvotes: 1

Related Questions