def creature_create(request, template_name="creatures/creature_edit.html"): if request.method == "POST": if "cancel" in request.POST: return HttpResponseRedirect(reverse("creatures")) form = CreatureForm(request.POST) if form.is_valid(): creature = form.save() return HttpResponseRedirect(reverse("creatures-details", args=(creature.id,))) else: form = CreatureForm() context = { "form": form } return direct_to_template(request, template_name, extra_context=context)
def creature_edit(request, creature_id, template_name="creatures/creature_edit.html"): creature = get_object_or_404(Creature, id=creature_id) if request.method == "POST": if "cancel" in request.POST: return HttpResponseRedirect(reverse("creatures")) form = CreatureForm(request.POST, instance=creature) if form.is_valid(): creature = form.save() return HttpResponseRedirect(reverse("creatures-details", args=(creature.id,))) else: form = CreatureForm(instance=creature) context = { "creature": creature, "form": form } return direct_to_template(request, template_name, extra_context=context)