def post(self, request, id): car = get_object_or_404(Car, pk=id) self.form = CarCreateForm(request.POST, request.FILES, instance=car) if self.form.is_valid(): self.form.save() return HttpResponseRedirect( reverse('car-detaiil', kwargs={'id': id})) return render(request, self.template_name, self.context)
def post(self, request): self.form = CarCreateForm(request.POST, request.FILES) if self.form.is_valid(): self.form.save() self.form = CarCreateForm self.context['form'] = self.form return HttpResponseRedirect(reverse('cars-settings')) return render(request, self.template_name, self.context)
class CarAdd(View): template_name = 'car_add.html' form = CarCreateForm context = {'form': form, 'title': 'Add car'} def get(self, request): return render(request, self.template_name, self.context) def post(self, request): self.form = CarCreateForm(request.POST, request.FILES) if self.form.is_valid(): self.form.save() self.form = CarCreateForm self.context['form'] = self.form return HttpResponseRedirect(reverse('cars-settings')) return render(request, self.template_name, self.context)
class CarEdit(View): template_name = 'car_edit.html' form = CarCreateForm context = { 'title': 'Car edit', } def get(self, request, id): car = get_object_or_404(Car, pk=id) self.form = CarCreateForm(None, instance=car) self.context['form'] = self.form return render(request, self.template_name, self.context) def post(self, request, id): car = get_object_or_404(Car, pk=id) self.form = CarCreateForm(request.POST, request.FILES, instance=car) if self.form.is_valid(): self.form.save() return HttpResponseRedirect( reverse('car-detaiil', kwargs={'id': id})) return render(request, self.template_name, self.context)
def user_profile(request): user = request.user if request.method == 'GET': if Car.objects.get(fk_user_id=user.id): get_car = Car.objects.get(fk_user_id=user.id) context = { 'user': user, 'results': Result.objects.filter(fk_user_id=user.id), 'form': CarCreateForm(), 'car': get_car, } return render(request, 'accounts/profile.html', context) if request.method == 'POST': car = Car.objects.get(fk_user_id=user.id) old_image = car.car form = CarCreateForm(request.POST, request.FILES, instance=car) if form.is_valid(): if old_image: delete_old_image(old_image.path) form.save() return redirect('user profile') return render(request, 'accounts/profile.html')
def persist_car(request, car, template_name): if request.method == 'GET': form = CarCreateForm(instance=cars) context = { 'form': form, 'car': car, } return render(request, f'{template_name}.html', context) else: form = CarCreateForm(request.POST, instance=cars) if form.is_valid(): form.save() return redirect('pages/services') context = { 'form': form, 'cars': cars, } return render(request, f'{template_name}.html', context)
def get(self, request, id): car = get_object_or_404(Car, pk=id) self.form = CarCreateForm(None, instance=car) self.context['form'] = self.form return render(request, self.template_name, self.context)