0

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!

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

My question or my script is maybe bad, but I have in my template two different things : a research form (in order to see if people exist or not) and a CreateForm. But both elements are independent. get_context_data is maybe not the good function ?
unfortunately, i don't understand you in full, may be you can add your logic instead of current code to the question.
Ok I will edit my question. But if I just want to make some researches on my database and display the result in my template with Class Based Views, How I can write that ?
i think you go by right way, i updated the answer, hope it help you more
Thank you @BearBrown. It's really obscur to myself. I don't really know How I have to handle that. I will make some tries and see if it works :/
1

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):

2 Comments

Thank you for Login part. But my initial question was : How I can have two different functions, independant on the same template :/
I suppose your form uses GET request to display the page and POST to create some data. so, move the complete GET part to the def get() and leave the creating part in form_valid() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.