From fb34c3f5aee76214197012b3e27d9a65604df7f2 Mon Sep 17 00:00:00 2001 From: David Morris Date: Thu, 16 May 2024 16:43:52 +0100 Subject: [PATCH] Adding form with deaths --- cmp/forms.py | 8 ++++++++ cmp/views.py | 16 ++++++++++++++-- templates/cmp/edit-soldiers.html | 3 +++ templates/cmp/mgmt-index.html | 11 +++++++---- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/cmp/forms.py b/cmp/forms.py index 809a77e..51f0ece 100644 --- a/cmp/forms.py +++ b/cmp/forms.py @@ -1,4 +1,5 @@ from django.contrib.auth.forms import UserCreationForm, UserChangeForm + from django import forms from .models import CustomUser @@ -8,6 +9,7 @@ from .models import Cemetery from .models import PowCamp from .models import Soldier +from .models import SoldierDeath class CustomUserCreationForm(UserCreationForm): @@ -51,3 +53,9 @@ class Meta: model = Soldier fields = "__all__" + +class editSoldierDeathForm(forms.ModelForm): + class Meta: + model = SoldierDeath + fields = ["date", "cemetery"] # replace 'date' with the actual name of your date field + diff --git a/cmp/views.py b/cmp/views.py index 7d53402..50dccd4 100644 --- a/cmp/views.py +++ b/cmp/views.py @@ -21,6 +21,9 @@ from .models import SoldierDecoration + +from .forms import editSoldierForm, editSoldierDeathForm + import folium from django.views.generic import TemplateView @@ -308,13 +311,22 @@ def edit_countries(request, country_id=None): def edit_soldiers(request, soldier_id): post = request.POST form = editSoldierForm(post or None) + death_form = None if soldier_id: soldier = Soldier.objects.get(id=soldier_id) form = editSoldierForm(post or None, instance=soldier) - if post and form.is_valid(): + try: + death = SoldierDeath.objects.get(soldier=soldier) + death_form = editSoldierDeathForm(post or None, instance=death) + except SoldierDeath.DoesNotExist: + death_form = editSoldierDeathForm(post or None) + + if post and form.is_valid() and (death_form is None or death_form.is_valid()): form.save() + if death_form is not None: + death_form.save() return HttpResponse("Soldier Added") - return render(request, "cmp/edit-soldiers.html", {"form": form}) + return render(request, "cmp/edit-soldiers.html", {"form": form, 'death_form': death_form}) diff --git a/templates/cmp/edit-soldiers.html b/templates/cmp/edit-soldiers.html index 97bdcd5..3cd21de 100644 --- a/templates/cmp/edit-soldiers.html +++ b/templates/cmp/edit-soldiers.html @@ -4,11 +4,14 @@ {% block content %}
+

Update/Edit Soldier Record

{% csrf_token %} {{ form.management_form }} {{ form.errors }} {{form | crispy}} +

Soldier Death Details

+ {{ death_form|crispy }}
diff --git a/templates/cmp/mgmt-index.html b/templates/cmp/mgmt-index.html index 83f53a5..8f2132b 100644 --- a/templates/cmp/mgmt-index.html +++ b/templates/cmp/mgmt-index.html @@ -1,5 +1,8 @@ - - +{% extends "base.html" %} +{% load static crispy_forms_tags %} +{% block title %}Admin Page{% endblock %} +{% block content %} + Management Index @@ -15,5 +18,5 @@

Broken

Working

  • Soldiers
  • - - \ No newline at end of file + + {% endblock %} \ No newline at end of file