Django的。如何以两种形式保存数据

时间:2014-05-14 10:42:25

标签: django

我有两种形式:

    class Form_registration_security (ModelForm):
    class Meta:
        model = Security
        fields = ['fk_id_users_security', 'e_mail', 'password']
        widgets = {
            'e_mail': forms.TextInput(attrs = {'placeholder': 'Your Email'}),
            'password': forms.TextInput(attrs = {'placeholder': 'New Password'}),
        }

class Form_registration_user (ModelForm):
    class Meta:
        model = Users
        fields = ['id', 'first_name', 'last_name', 'date_birthdaty']
        widgets = {
            'id': forms.TextInput(attrs = {'placeholder': 'id'}),
            'first_name': forms.TextInput(attrs = {'placeholder': 'First Name'}),
            'last_name': forms.TextInput(attrs = {'placeholder': 'Last Name'}),
            'date_birthdaty': forms.TextInput(attrs = {'placeholder': 'Date'})
        }

但数据仅在一种模式下保存 - (Form_registration_user)。

视野中的代码:

def save_registration (request ):
if request.method == 'POST':
    form_user = Form_registration_user(request.POST)
    form_security = Form_registration_security(request.POST)
    if form_user.is_valid() and form_security.is_valid():
        data_user =  form_user.save()
        data_security = form_security.save(commit=False)
        data_security.data_user = data_user
        data_security.save()
        return render_to_response('see_you_later.html')
    else:
        return render_to_response('error.html')

我总是看到 - error.html,虽然我填的是正确的表格。

模型用户有一个主键。 Model Security有一个外键。

我的模板:

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div class="entry_or_register">
{% load staticfiles %}
<img src="{% static "tumblr.gif" %}" width="1250" height="550">


<form name="registration" method="post" action="save/">
{% csrf_token %}
{{ form_registration_user.as_p }}
{{ form_registration_security.as_p }}

<input type="submit" value="SignUp">
</form>
</div>
</body>
</html>

感谢您的帮助!正确的功能:

def save_registration (request ):
if request.method == 'POST':
    form_user = Form_registration_user(request.POST)
    form_security = Form_registration_security(request.POST, request.FILES)
    if form_user.is_valid():
        data_user =  form_user.save()
        data_security = form_security.save(commit=False)
        data_security.data_user = data_user
        data_security.save()
        return render_to_response('see_you_later.html')
    else:
        return render_to_response('error.html')

2 个答案:

答案 0 :(得分:0)

我不确定您是如何在模板中呈现表单的,但可能是当您单击“提交”时,只有其中一个表单在HTTP请求中发送其数据。

然后另一个表单的构造函数在POST变量中找不到它的键,结果不会是一个有效的表单。我认为你测试的原因总是失败。

现在,我希望你能给我们一些关于你正在尝试做什么的更多细节,但我认为你将需要一个自定义的Form类(这将是你当前两种形式的结合而不是ModelForm。 编辑:抱歉,您实际上不应该这样做......

祝你好运。

答案 1 :(得分:0)

您还应该发布相应模板的html标记。

无论如何,这是我用过的一个视图,我必须在同一页面中保存来自两个ModelForms的数据,用户点击一个提交按钮:

def register(request):

message = None
if request.method == 'POST':
    user_form = NewUserForm(request.POST)
    details_form = UserDetailsForm(request.POST, request.FILES)
    if user_form.is_valid():
        new_simple_user = user_form.save()
        message = _("User inserted")
        if details_form.is_valid():
            # Create, but don't save the new user details instance.
            new_user_details = details_form.save(commit=False)
            # Associate the user to the user details
            new_user_details.user = new_simple_user
            # save a new user details instance
            new_user_details.save()
            message = _("User details inserted")


else:
    user_form = NewUserForm()
    details_form = UserDetailsForm()
return render_to_response('register.html', { 'user_form': user_form, 'details_form': details_form, 'message': message,},\
                        context_instance=RequestContext(request))