def categoria_new(request): context = {'label': 'Cadastrar', 'form': CategoriaVeiculoForm()} if request.method == 'GET': return render(request, 'categoria_veiculo/categoria_veiculos.html', context) form = CategoriaVeiculoForm(request.POST) context['form'] = form if not form.is_valid(): return render(request, 'categoria_veiculo/categoria_veiculos.html', context) form.save() messages.success(request, SUCCESS_MSG) return HttpResponseRedirect(r('categoria'))
def categoria_edit(request, pk): cat = get_object_or_404(CategoriaVeiculo, pk=pk) if request.method == "POST": form = CategoriaVeiculoForm(request.POST, instance=cat) if form.is_valid(): cat = form.save(commit=False) cat.save() messages.success(request, UPDATE_MSG) return HttpResponseRedirect(r('categoria')) else: form = CategoriaVeiculoForm(instance=cat) context = {'label': 'Editar', 'form': form} return render(request, 'categoria_veiculo/categoria_veiculos.html', context)