Skip to content

Commit

Permalink
Merge pull request #162 from gm3dmo/formdeath
Browse files Browse the repository at this point in the history
Adding form with deaths
  • Loading branch information
gm3dmo authored May 16, 2024
2 parents 1ecb3dd + fb34c3f commit ecadcb2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
8 changes: 8 additions & 0 deletions cmp/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.auth.forms import UserCreationForm, UserChangeForm

from django import forms

from .models import CustomUser
Expand All @@ -8,6 +9,7 @@
from .models import Cemetery
from .models import PowCamp
from .models import Soldier
from .models import SoldierDeath


class CustomUserCreationForm(UserCreationForm):
Expand Down Expand Up @@ -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

16 changes: 14 additions & 2 deletions cmp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

from .models import SoldierDecoration


from .forms import editSoldierForm, editSoldierDeathForm

import folium
from django.views.generic import TemplateView

Expand Down Expand Up @@ -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})



Expand Down
3 changes: 3 additions & 0 deletions templates/cmp/edit-soldiers.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
{% block content %}

<div>
<h1 class="mgmt-title">Update/Edit Soldier Record </h1>
<form method="POST">
{% csrf_token %}
{{ form.management_form }}
{{ form.errors }}
{{form | crispy}}
<h2 class="mgmt-title">Soldier Death Details </h2>
{{ death_form|crispy }}
<button type="submit" class="btn btn-primary">Save</button>
</form>

Expand Down
11 changes: 7 additions & 4 deletions templates/cmp/mgmt-index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<!DOCTYPE html>
<html>
{% extends "base.html" %}
{% load static crispy_forms_tags %}
{% block title %}Admin Page{% endblock %}
{% block content %}

<head>
<title>Management Index</title>
</head>
Expand All @@ -15,5 +18,5 @@ <h3>Broken</h3>
<h3>Working</h3>
<li><a href="/mgmt/soldiers/search/">Soldiers</a></li>
</ul>
</body>
</html>

{% endblock %}

0 comments on commit ecadcb2

Please sign in to comment.