如何将“request.POST”从视图传递到Django中的函数

时间:2015-08-17 09:56:07

标签: django forms django-forms django-views views

问题已解决,代码反映了解决方案

对于某种网店(它有点复杂,但有助于这个想法)我已经基于相同的模型创建了四种不同的形式。 根据产品,我想显示一些字段并隐藏其他字段,并在必要时设置所需。 注意:在一个视图中它不是多个表单,我只想显示四种表单中的一种

在视图中我有以下代码:

def add_regelset(request, bon_id, product_id):
    bon = get_object_or_404(Bon, pk=bon_id)
    product = get_object_or_404(Product, pk=product_id)

    if request.method == 'POST':
        form = form_choice(product, None, request.POST)
        if form.is_valid():
            formregelset = form.save(commit=False)
            formregelset.bon_id = bon.id
            formregelset.save()
            # Haal de nieuwe regelset op en maak de nodige regels aan
            regelset = get_object_or_404(Regelset, id=formregelset.id)
            maak_regelset.delay(regelset.id)
            return HttpResponseRedirect(reverse('calculator:bondetails', args=(regelset.bon.id,)))
        else:
            print(form.errors)
    else:
        form = form_choice(product, False, False)

    context_dict = {'form': form, 'bon': bon, 'product': product}

    return render(request, 'calculator/regelset/regelset_add.html', context_dict)


def alter_regelset(request, regelset_id):
    regelset = get_object_or_404(Regelset, pk=regelset_id)
    product = regelset.product
    bon = get_object_or_404(Bon, pk=regelset.bon_id)

    if request.method == 'POST':
        form = form_choice(product, regelset, request.POST)
        if form.is_valid():
            form.save()
            wijziging_regelset.delay(regelset.id)
            return HttpResponseRedirect(reverse('calculator:bondetails', args=(bon.id,)))

        else:
            print(form.errors)
    else:
        form = form_choice(product, regelset, False)

    context_dict = {'form': form, 'regelset': regelset}
    return render(request, 'calculator/regelset/regelset_wijzig.html', context_dict)

说明:
Regelset =链接到表格的模型

form_choice:

def form_choice(product, instance, postdata):
    toepassing = product.toepassing
    afmeting = toepassing.afmeting_verplicht()

    # bepaal initial
    initial = None
    if not instance:
        instance = None
    if instance is None:
        initial = {'type_korting': MenusGlobals.KORT_PROC,
                                         'toepassing': toepassing,
                                         'product': product,
                                         'omslag_dubbelzijdig': product.omslag_dubbelzijdig,
                                         'binnenwerk_dubbelzijdig': product.binnenwerk_dubbelzijdig,
                                         'naam': product.naam,
                                         'originelen': product.get_originelen(),
                                         'oplage': product.get_oplage()}

        if toepassing.form_select == MenusGlobals.XL_PRINT:
            if afmeting:
                initial.update({'breedte_in_cm': product.get_breedte_in_cm(),
                                'hoogte_in_cm': product.get_hoogte_in_cm()})

        elif toepassing.form_select == MenusGlobals.REPRO:
            initial.update({'omslag_dubbelzijdig': product.omslag_dubbelzijdig,
                            'binnenwerk_dubbelzijdig': product.binnenwerk_dubbelzijdig,
                            'breedte_in_cm': product.get_breedte_in_cm(),
                            'hoogte_in_cm': product.get_hoogte_in_cm(),
                            'breedte_in_cm2': product.get_breedte_in_cm2(),
                            'hoogte_in_cm2': product.get_hoogte_in_cm2()})

    # bepaal form
    form = RegelsetDataFormXLprintZonderAfmeting(initial=initial, instance=instance)
    if postdata:
        form = RegelsetDataFormXLprintZonderAfmeting(postdata, initial=initial, instance=instance)

    if toepassing.form_select == MenusGlobals.XL_PRINT:
        if afmeting:
            form = RegelsetDataXLprint(initial=initial, instance=instance)

    elif toepassing.form_select == MenusGlobals.REPRO:
        alleenbinnenwerk = False
        alleenomslag = False
        for r in toepassing.toepassingsregel_set.all():
            if r.artikelgroep.repro_omslag:
                alleenomslag = True
            if r.artikelgroep.repro_binnenwerk:
                alleenbinnenwerk = True

        if alleenbinnenwerk and alleenomslag:
            form = RegelsetDataReproBinnenwerkOmslag(initial=initial, instance=instance)
        elif alleenbinnenwerk:
            form = RegelsetDataReproBinnenwerk(initial=initial, instance=instance)
        elif alleenomslag:
            form = RegelsetDataReproOmslag(initial=initial, instance=instance)

    return form

到目前为止这么好......现在问题。我希望在request.method"POST"时使用相同的form_choice函数来定义表单变量。

到目前为止,我尝试过将{。kwarg中的request.POST设置为arg并将form_choice设置为{/ 1}}

form = form_choice(product, request=request)

并使用request参数返回正确的表单,但结果为SyntaxError,我不知道原因。

任何人都可以帮助我吗?

更新代码:反映解决方案

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我使用* args和** kwargs来传递request.POST导致原始数据被放置在元组或字典中的原因。

对不起,不好意思。我将编辑帖子以反映包括表单选择方法

在内的解决方案