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})
Community
  • 1
  • 1
Nancy
  • 3,849
  • 4
  • 24
  • 47
  • 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? – Wtower May 19 '15 at 07: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? – Nancy May 19 '15 at 14:30

2 Answers2

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})
Wtower
  • 15,424
  • 11
  • 94
  • 69
  • 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`? – Alexey Milogradov May 19 '15 at 14:54
  • I would strongly recommend you to open a new question with all details and I will be eager to help you. – Wtower May 19 '15 at 14:56
  • Here is my related question http://stackoverflow.com/questions/30329804/one-html-form-several-interrelated-django-forms-how-to-save – Alexey Milogradov May 19 '15 at 15:25
  • 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 May 19 '15 at 15:43
  • @Nancy I am glad! You too post another question if you like, sounds to me you are in for AJAX. – Wtower May 20 '15 at 07:14
  • @Wtower I think I might have to post about the dropdown thing, too. I made progress but got stuck again. – Nancy May 20 '15 at 14:08
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.

taesu
  • 4,250
  • 1
  • 19
  • 40