Switch case for form_class and template_name

倖福魔咒の 提交于 2019-12-11 08:05:18

问题


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


回答1:


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



回答2:


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




回答3:


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)


来源:https://stackoverflow.com/questions/19813052/switch-case-for-form-class-and-template-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!