Nancy
Nancy

Reputation: 4089

2 forms, 1 view, 2 SQL tables in Django

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

Upvotes: 3

Views: 270

Answers (2)

Wtower
Wtower

Reputation: 19902

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

Upvotes: 1

taesu
taesu

Reputation: 4570

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.

Upvotes: 0

Related Questions