def edit_pet(request, pk): pet = Pet.objects.get(pk=pk) if request.method == 'POST': form = PetCreateForm(request.POST, instance=pet) if form.is_valid(): pet = form.save() pet.save() return redirect('pet-detail', pk=pk) return HttpResponse('not correct') context = {'form': PetCreateForm(instance=pet)} return render(request, 'pets/pet_edit.html', context)
def create_pet(request): if request.method == 'POST': form = PetCreateForm(request.POST) if form.is_valid(): pet = form.save() pet.save() return redirect('pet_all') context = {'form': form} return render(request, 'pets/pet_create.html', context) context = {'form': PetCreateForm()} return render(request, 'pets/pet_create.html', context)
def pet_create(request): form = PetCreateForm() if request.method == 'POST': form.__init__(request.POST) if form.is_valid(): form.save() return redirect('all_pets') return render(request, 'pets/pet_create.html', {'form': form})
def pet_edit(request, pk): pet = Pet.objects.get(pk=pk) if request.method == 'POST': form = PetCreateForm(request.POST, instance=pet) if form.is_valid(): form.save() return redirect('pet_detail', pk) form = PetCreateForm(instance=pet) return render(request, 'pets/pet_edit.html', {'form': form})
def create_pet(request, pet_form=None, form_action='create pet', pk=None): if request.method == 'GET': if not pet_form: pet_form = PetCreateForm() context = { 'pet_form': pet_form, 'form_action': form_action, 'pk': pk, } return render(request, 'pet_create.html', context) else: pet_form = PetCreateForm(request.POST, request.FILES) if pet_form.is_valid(): pet = pet_form.save() pet.save() return redirect('all pets') context = { 'pet_form': pet_form } return render(request, 'pet_create.html', context)
def edit_pet(request, pk): pet = Pet.objects.get(pk=pk) if request.method == 'GET': pet_form = PetCreateForm(initial=pet.__dict__) context = { 'pet': pet, 'pet_form': pet_form, } return render(request, 'pet_edit.html', context) else: pet_form = PetCreateForm(request.POST, request.FILES) if pet_form.is_valid(): pet = pet_form.save() pet.save() return redirect('all pets') return create_pet( request, pet_form=pet_form, form_action='edit pet', pk=pk )