سؤال

I have two forms:

    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'})
        }

But data saves only in one mode - (Form_registration_user).

Code in view:

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')

I'm always see - error.html, although I'm fill right form.

Model User have a primary key. Model Security have a foreign key.

My template:

<!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>

Thanks for help! Right function:

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')
هل كانت مفيدة؟

المحلول

You should post also the html markup of the corresponding template.

Anyway, here's a view i used once i had to save data from two ModelForms in the same page, with the user clicking a single submit button:

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))

نصائح أخرى

I'm not sure how you rendered your forms in the template, but it could be that when you click submit, only one of the forms sends its data in the HTTP request.

Then the other form's constructor won't find its key in the POST variable and the outcome won't be a valid form. I think that's why you test always fail.

Now, I hope you could give us some more details on what you're trying to do but I think you are going to need a custom Form class (that would be the union of your two current forms) instead of a ModelForm. EDIT : sorry, you shouldn't actually need to do that...

Good luck.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top