You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django图书管理系统归还功能表单提交但数据未存入数据库求助

Hey, let's break down why your return book data isn't saving to the database—there are a few key issues in your code that are blocking this:

1. Form Submission Route Mismatch

Your form's action points to the returned-book route (linked to the TransactionandReturnBook view), but all your form processing logic lives in the returnBook view. When users click "Return Book", the submit request sends data to TransactionandReturnBook instead of returnBook—if that view doesn't handle form saving, nothing gets written to the database.

Fix:
Update your form's action to point to the current return-book route (you'll need to pass the obj to the template to access its ID):

<!-- In returnbookform.html -->
<form method="POST" action="{% url 'return-book' pk=obj.id %}">

And add obj to your view's context so it's available in the template:

# In returnBook view
context = { 'form': form, 'obj': obj }
return render(request, 'lms/returnbook_form.html', context)

2. Broken Initial Date Value

You're passing datetime.today (the function itself) as the initial value for actual_return_date, instead of calling it to get the current date. This will cause validation issues and prevent the form from being marked as valid.

Fix:

initial_dict = {
    'book': obj.book,
    'student_name': obj.student_name,
    'actual_return_date': datetime.today(),  # Add parentheses to call the function
    'fine_amount': fine,  # Use the calculated fine variable instead of obj.fine_amount (Issue has no fine_amount field!)
}

3. Incorrect Fine Calculation

Your current logic for calculating overdue days (diff_date = d2.day - d1.day) is flawed—it doesn't account for cross-month dates (e.g., returning on May 15 when the due date was April 30 would give a wrong value).

Fix:
Calculate the actual day difference properly:

d1 = date.today()
d2 = obj.expected_return_date
diff_days = (d1 - d2).days
fine = 0
if diff_days > 0:
    fine = diff_days * 2

4. Unsafe Student Lookup

Using Student.objects.get(first_name=fname) is risky—multiple students could share the same first name. You don't need to query at all, since obj.student_name is already the linked Student object.

Fix:

# Remove these lines:
# fname = obj.student_name.first_name
# student = Student.objects.get(first_name=fname)

# Use the existing foreign key directly:
student = obj.student_name
student.no_of_issued_books -= 1
student.save()

5. Premature Issue Record Deletion

You're deleting the Issue record before checking if the form is valid. This means even if the form submission fails, the borrow record is gone—bad for data integrity and user experience.

Fix:
Move the deletion inside the form.is_valid() block so it only runs when the return record saves successfully:

if form.is_valid():
    form.save()
    # Only delete the issue after confirming the return is saved
    Issue.objects.filter(id=obj.id).delete()
    messages.success(request, 'You have returned the book')
    return redirect('returned-book')

After applying these fixes, test the return flow again. Also, double-check your TransactionandReturnBook view to make sure it's only handling display of returned books (not form submissions) since we're routing the form back to returnBook.

内容的提问来源于stack exchange,提问作者Nitish Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 15:17:47