문제

I have a session var with the name 'foo'. Now I would like, depending of the value of 'foo' load a specific form and template in my cbv. So I need to put form_class and template_name into a switch case.

Which function is the right place for this? get? get_form? looks like nothing is really the right place for this.

Anyone a suggestion or knows another way? :)

도움이 되었습니까?

해결책

CBV explorer is your friend: http://ccbv.co.uk/projects/Django/1.5/django.views.generic.edit/CreateView/

You need to override

def get_form_class(self):

and

def get_template_names(self):

다른 팁

after asking my FormView CBV change ModelForm per request of very much like this question just without the template changing I found this one and your grate answers

so basically I am going to sum it all up at first I tested this answer by Denny Crane

class Foo(FormView):
    def dispatch(self, request, *args, **kwargs):
        self.var = request.session['sessionvar']['var']
        if self.var == some_value:
            form_class = form1
            template_name = template1
        elif self.var == another_value:
            form_class = form2
            template_name = template2
        [...]
        return super(Foo, self).dispatch(request, *args, **kwargs)

I did needed to override

def get_form_class(self):

and

def get_template_names(self):

for this to work and exactly what I needed JUST WITHOUT THE TEMPLATE part becouse in my situation I would like to keep the same templae so the combination of that two did work! however then I sew @Serafeim comment

Be careful - this is not good usage of django! If you don't want to repeat your conditions in both functions just define a new function that would contain > your conditions and would return True/False. Now this function could be used from > > both get_form_class and get_template_names :)

i changed everything to

this code only

def get_form_class(self):
    self.step = self.request.GET.get('step') 
    # for now I am getting this with request.get, till I will get the 
    # grip on session :) my first week on django and web-dev in general 
    if not self.step:
        self.step = 'step1'
    self.form_class = FORM[self.step] #FORM = dict{'step#': ModelForm}
    return self.form_class

and this is working answer thanks all

If I would use get_form_class() and get_template_names I would have to repeat in both functions my conditions. That would be redundant. Which is not prefered.

I figured out another solution which is not repeating code fragments.

class Foo(FormView):
    def dispatch(self, request, *args, **kwargs):
        self.var = request.session['sessionvar']['var']
        if self.var == some_value:
            form_class = form1
            template_name = template1
        elif self.var == another_value:
            form_class = form2
            template_name = template2
        [...]
        return super(Foo, self).dispatch(request, *args, **kwargs)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top