从参数数据获取字段值和直接从request.POST获取字段值之间的ModelForm实例有什么区别?

时间:2016-06-07 08:15:22

标签: django django-models django-forms

这是我的代码:

from .forms import ImageCreateForm


@login_required
def image_create(request):
    """
    View for creating an Image using the JavaScript Bookmarklet.
    """
    if request.method == 'POST':
        # form is sent
        # ########  here is my issue ####################
        form = ImageCreateForm(data=request.POST)
        # ########  end of  my issue ####################
        if form.is_valid():
            ......
            # redirect to new created item detail view
            return redirect(new_item.get_absolute_url())
    else:
        # build form with data provided by the bookmarklet via GET
        form = ImageCreateForm(data=request.GET)

    return render(request, 'images/image/create.html', {'section': 'images', 'form': form})

这段代码来自Django的例子,但是当我在Django的官方网站上时,我读到了Modleform的一个实例是这样创建的: https://docs.djangoproject.com/en/1.9/topics/forms/modelforms

save()方法

# Create a form instance from POST data.
>>> f = ArticleForm(request.POST)

所以这是我的问题:从POST数据创建表单实例的两种方法之间有什么区别

1 个答案:

答案 0 :(得分:1)

形式__init__函数的第一个位置参数是数据,所以我们可以在位置上传递它

export PYTHONPATH=/opt/ros/indigo/lib/python2.7/site-packages

或通过关键字

f = ArticleForm(request.POST)

感谢Daniel Roseman告诉我这个。