3

I'm struggling to understand how to submit data from two django forms into two separate database tables from the same view. I only want one submit button. While this question got me closer to the solution, I'm getting errors and the data is not writing to the database. I think this code actually checks the two forms against each other instead of submitting both forms in one go. Any ideas?

Here's what I've tried:

For one form --> one table. This works, so it's a start.

# views.py
def BookFormView(request):
    if request.method == 'POST':
    form = BookForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/books/")
    else:
        form = BookForm()
    return render(request, 'books/createbooks.html',
              {'form' : form})

However, when I add this form in from forms.py to get the subsequent views.py I get local variable 'book_form' referenced before assignment. That's usually an easy global-vs-local variable issue to fix, but I don't know what it means in this case.

def BookFormView(request):
    if request.method == 'POST':
        if 'book' in request.POST:
            book_form = BookForm(request.POST, prefix='book')
            if book_form.is_valid():
                book_form.save()
                return HttpResponseRedirect("/books/")

            bookdetailsform = BookDetailsForm(prefix='bookdetails')
        elif 'bookdetails' in request.POST:
            bookdetailsform = BookDetailsForm(request.POST, prefix='bookdetails')
            if bookdetailsform.is_valid():
                bookdetailsform.save()
                return HttpResponseRedirect("/books/")

            book_form = BookForm(prefix='book')
    else:
        book_form = BookForm(prefix='book')
        bookdetailsform = BookDetailsForm(prefix='bookdetails')
    return render(request, 'books/createbook.html',
            {'book_form' : book_form,
             'bookdetailsform': bookdetailsform})
2
  • Why do you have two separate ifs when posting (book and bookdetail) since you only need one logical path, that is to save both forms? Commented May 19, 2015 at 7:30
  • Interesting. The honest answer is that I don't know; I was trying to use other SO answers to piece together something that would work. What I don't understand is how to only use one "if". See what I mean? Commented May 19, 2015 at 14:30

2 Answers 2

1

Based on the question's comments:

def BookFormView(request):
    if request.method == 'POST':
            book_form = BookForm(request.POST, prefix='book')
            bookdetailsform = BookDetailsForm(request.POST, prefix='bookdetails')
            if book_form.is_valid() and bookdetailsform.is_valid():
                book_form.save()
                bookdetailsform.save()
                return HttpResponseRedirect("/books/")
    else:
        book_form = BookForm(prefix='book')
        bookdetailsform = BookDetailsForm(prefix='bookdetails')
    return render(request, 'books/createbook.html', 
                  {'book_form': book_form, 'bookdetailsform': bookdetailsform})
Sign up to request clarification or add additional context in comments.

6 Comments

I have problem similar to the initial, and worked around similar solution, but now struggling with one more question. If the second form should have foreign field to the instance returned by the first form, how do you suggest to pass it there? In the terms of example: BookDetails model should have ForeignKey to Book. Considering book = book_form.save(), how to pass book to bookdetailsform?
I would strongly recommend you to open a new question with all details and I will be eager to help you.
Here is my related question stackoverflow.com/questions/30329804/…
Awesome, this makes a lot of sense. I realized that the bookdetailsform has some fields that are going to be auto fill or dropdown fields for genre categories, so I need to figure out how to do that before I can make sure everything is posting correctly. In the mean time, I'm marking this correct because it resolved the two-button and two-if's issue.
@Nancy I am glad! You too post another question if you like, sounds to me you are in for AJAX.
|
0

I think the problem is that when a user submits a bookdetails post request,
it will be handled under if 'book' in request.POST: condition. Why?
because string bookdetails contains string book, no matter the type of request they do, it will be handled with if book in request.POST: condition.

I believe fixing that if condition problem is the first step.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.