Stay on form after adding things

This commit is contained in:
2025-12-28 22:58:01 +01:00
parent f6a953158d
commit 805510c0f6
2 changed files with 11 additions and 8 deletions

View File

@@ -210,6 +210,12 @@
</table> </table>
</form> </form>
{% endif %} {% endif %}
{% if success_message %}
<div class="success-message">
{{ success_message }}
</div>
{% endif %}
</div> </div>
</body> </body>
</html> </html>

View File

@@ -62,6 +62,8 @@ def add_things(request, box_id):
"""Add multiple things to a box at once.""" """Add multiple things to a box at once."""
box = get_object_or_404(Box, pk=box_id) box = get_object_or_404(Box, pk=box_id)
success_message = None
if request.method == 'POST': if request.method == 'POST':
formset = ThingFormSet(request.POST) formset = ThingFormSet(request.POST)
@@ -73,17 +75,12 @@ def add_things(request, box_id):
thing.box = box thing.box = box
thing.save() thing.save()
created_count += 1 created_count += 1
print(f'DEBUG: created_count={created_count}')
if created_count > 0: if created_count > 0:
print(f'DEBUG: Redirecting to box_detail with box_id={box_id}') success_message = f'Added {created_count} thing{"s" if created_count > 1 else ""} successfully.'
return redirect('box_detail', box_id=box_id)
else:
print('DEBUG: No valid data submitted')
else:
formset = ThingFormSet() formset = ThingFormSet()
return render(request, 'boxes/add_things.html', { return render(request, 'boxes/add_things.html', {
'box': box, 'box': box,
'formset': formset, 'formset': formset,
'success_message': success_message,
}) })