django向导,在同一步骤中使用form和formset [英] django wizard, using form and formset in the same step

查看:172
本文介绍了django向导,在同一步骤中使用form和formset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我想要开始编码的场景,我正在考虑使用 django向导

I have a scenario which I'm trying to plan to start coding and I'm thinking to use django wizard.

我的计划是用两个步骤构建一个django向导,第一个简单但第二个更复杂一些。第二步将包含一个基于从第一步中选择的价值重塑的表格,我可以看到自己在做。我已经探索了所有现有的功能,我认为它可以轻松完成。

My plan is to build a django wizard with two steps, the first simple but the second a bit more complicated. The second step will contain a form that will reshape based on value selected from the first step, which I can see myself doing. I already explored all the existing functionality and I think it can be done easily.

我面临的挑战是,在第二步本身。我有一个表单和一个表单,表单是一个到多个(文章 - >图像),所以当达到第二步,用户将能够上传一个或多个图像到同一篇文章。

The challenge I'm facing though, is that in the second step itself. I have a form and a formset, the formset is one to many to form (Article -> Images) so when reaching the second step the user will be able to upload one or more images to the same article.

我试图在google邮寄列表和stackoverflow上搜索到,以将表单集传递给django向导类,但是看起来您不能在同一步骤中传递两种表单。

I tried to search everywhere on google mailing lists and stackoverflow for passing a formset to the django wizard class but it seems like you can not pass two forms in the same step.

NewItemWizard.as_view([
    ('category',    CategorySelectionForm),
    ('article',        ArticleForm)
])

如上图所示,我想能够将ArticleForm和ImageFormset都传递到第二步。有没有办法开箱即用?

as seen, in the example code above, I would like to be able to pass both ArticleForm and ImageFormset to the second step. Is there a way to do this out of the box?

根据我正在阅读的内容,我相信使用像get_context_data这样的函数可以帮助,但是会非常麻烦。

Based on what I'm reading, I believe using a function like get_context_data could help, but it will be very hacky.

def get_context_data(self, form, **kwargs):
    context = super(NewItemWizard, self).get_context_data(form=form, **kwargs)
    if self.steps.current == 'article':
        context.update({
            'image_formset': ImageFormset()
        })
    return context

任何人都可以建议更好的方法?

Anyone can advise for a better approach?

干杯,

推荐答案

没有直接的API但是,这里有一个清晰的解决方案:

There is no straight API to do it yet, but there is a clean solution like the one mentioned here:

https://code.djangoproject.com/ticket/18830

这篇关于django向导,在同一步骤中使用form和formset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆