将初始数据从数据库传递给Django表单向导模板

时间:2021-10-16 09:47:13

This is the first time I have to use the Django Wizard Form and I'm having more problems than expected (I'm a newbie). I'm trying to pass some initial data from a database to a Form Wizard template. I have been googling but I have found no answer.

这是我第一次使用Django向导形式,而且我遇到的问题比预期的要多(我是新手)。我试图将一些初始数据从数据库传递到表单向导模板。我一直在谷歌上搜索,但没有找到答案。

I think that I must override the get_context_data method to query my database and build and return a dictionary, but I'm not really sure about that. Could someone explain me the correct process?

我认为我必须重写get_context_data方法来查询我的数据库并构建并返回一个字典,但我对此并不确定。谁能给我解释一下正确的过程吗?

I put my code below to show you my idea.

我把我的代码放在下面向你展示我的想法。

def my_function(request):
    form = MyWizard.as_view([Form1, Form2, Form3])
    return form(context=RequestContext(request), request=request)

TEMPLATES = {'0': "form1.html",
             '1': "form2.html",
             '2': "form3.html",}

class MyWizard(SessionWizardView):

    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]

    def get_context_data(self, form, **kwargs):
        if self.steps.current == 0:

            list = MyModel.objects.all()   # query my database

            # convert list to dict

            return context   # return context with the dict

    def done(self, form_list, **kwargs):
        form_data = process_form_data(self.request, form_list)

        return render_to_response('process-completed.html', {'form_data': form_data,})

Thank you.

谢谢你!

2 个解决方案

#1


2  

This example specifies how to pass initial data to the wizard.

此示例指定如何将初始数据传递给向导。

In your case you need more complex processing, so you should override get_form_initial() method, e.g.:

在您的情况中,您需要更复杂的处理,所以您应该覆盖get_form_initial()方法,例如:

def get_form_initial(self, step):
    initial = {}
    if step == 1:
        client_pk = self.kwargs.get('client_pk')
        initial.update({
            'client': Client.objects.get(pk=client_pk)
        })
    return self.initial_dict.get(step, initial)

#2


0  

Thank you very much!! I write my code below to show how I solved my problem based on your code.

非常感谢! !我在下面编写代码,以展示如何基于您的代码解决问题。

def get_form_initial(self, step):
    initial = {}
    if step == '0':
        t = MyModel.objects.all() # Get the list of objects of my database
        initial = {x.id: x.name for x in t} # Convert this list to python dictionary
    return self.initial_dict.get(step, initial)

Hope this is useful.

希望这是有用的。

#1


2  

This example specifies how to pass initial data to the wizard.

此示例指定如何将初始数据传递给向导。

In your case you need more complex processing, so you should override get_form_initial() method, e.g.:

在您的情况中,您需要更复杂的处理,所以您应该覆盖get_form_initial()方法,例如:

def get_form_initial(self, step):
    initial = {}
    if step == 1:
        client_pk = self.kwargs.get('client_pk')
        initial.update({
            'client': Client.objects.get(pk=client_pk)
        })
    return self.initial_dict.get(step, initial)

#2


0  

Thank you very much!! I write my code below to show how I solved my problem based on your code.

非常感谢! !我在下面编写代码,以展示如何基于您的代码解决问题。

def get_form_initial(self, step):
    initial = {}
    if step == '0':
        t = MyModel.objects.all() # Get the list of objects of my database
        initial = {x.id: x.name for x in t} # Convert this list to python dictionary
    return self.initial_dict.get(step, initial)

Hope this is useful.

希望这是有用的。