I am trying to create an edit form to edit current objects using Django. I am having trouble trying to get the current id of the object in order to set the initial value to the current values of the object.
I want to create an edit form that will already show the current data before the user edits a field. How do I go about doing this?
Thanks.
my forms.py:
class AddfooditemForm(forms.Form): quantity = forms.CharField(label="Quantity") price_per_pound = forms.CharField(label="price_per_pound") expiration_date = forms.CharField(label="expiration_date") price_per_item = forms.CharField(label="price_per_item") class AddNonFooditemForm(forms.Form): quantity = forms.CharField(label="Quantity") price_per_item = forms.CharField(label="price_per_item") class EditfooditemForm(ModelForm): quantity = forms.CharField(label="Quantity") price_per_pound = forms.CharField(label="price_per_pound") expiration_date = forms.CharField(label="expiration_date") price_per_item = forms.CharField(label="price_per_item") def __init__(self, *args, **kwargs): super(EditfooditemForm, self).__init__(*args, **kwargs) class Meta: model = tasks fields = ['quantity', 'price_per_item', 'expiration_date', 'price_per_pound'] class Edit_non_food_itemForm(ModelForm): quantity = forms.CharField(label="Quantity") price_per_item = forms.CharField(label="price_per_item") def __init__(self, *args, **kwargs): super(Edit_non_food_itemForm, self).__init__(*args, **kwargs) class Meta: model = tasks fields = ['quantity', 'price_per_item'] my views.py:
@csrf_exempt def add_item(request): if request.method == 'POST' and 'add_food_form' in request.POST: add_food_form = AddfooditemForm(request.POST) if add_food_form.is_valid(): # Cleaned_data input_type = 'food' quantity = add_food_form.cleaned_data['quantity'] price_per_pound = add_food_form.cleaned_data['price_per_pound'] expiration_date = add_food_form.cleaned_data['expiration_date'] price_per_item = add_food_form.cleaned_data['price_per_item'] foodDict = {'price_per_item': price_per_item, 'quantity': quantity, 'price_per_pound': price_per_pound, 'expiration_date': expiration_date} foodData = pickle.dumps(foodDict) item = items(input_type=input_type, foodData=foodData) item.save() return HttpResponseRedirect(reverse('backup_app.views.items_listing')) if request.method == 'POST' and 'add_non_food_form' in request.POST: add_non_food_form = AddNonFooditemForm(request.POST) if add_non_food_form.is_valid(): # Cleaned_data input_type = 'non_food' quantity = add_non_food_form.cleaned_data['quantity'] price_per_item = add_non_food_form.cleaned_data['price_per_item'] non_foodDict = {'quantity': quantity, 'price_per_item': price_per_item} non_foodData = pickle.dumps(non_foodDict) item = items(input_type=input_type, non_foodData=non_foodData) item.save() return HttpResponseRedirect(reverse('backup_app.views.items_listing')) else: 'add_food_form': AddfooditemForm() 'add_non_food_form': AddNonFooditemForm() return render(request, 'backup_app/items_listing.html', {'add_food_form': add_food_form,'add_non_food_form': add_non_food_form}) @csrf_exempt def edit_item(request, item_id): item = items.objects.get(id=item_id) if request.method == 'POST' and 'edit_food_form' in request.POST: edit_food_form = EditfooditemForm(request.POST, instance=item) if edit_food_form.is_valid(): print "valid" # Cleaned_data item.input_type = 'food' quantity = edit_food_form.cleaned_data['quantity'] price_per_pound = edit_food_form.cleaned_data['price_per_pound'] expiration_date = edit_food_form.cleaned_data['expiration_date'] price_per_item = edit_food_form.cleaned_data['price_per_item'] foodDict = {'price_per_item': price_per_item, 'quantity': quantity, 'price_per_pound': price_per_pound, 'expiration_date': expiration_date} item.foodData = pickle.dumps(foodDict) item.save() return HttpResponseRedirect(reverse('backup_app.views.items_listing')) if request.method == 'POST' and 'edit_non_food_form' in request.POST: edit_non_food_form = Edit_non_food_itemForm(request.POST, instance=item) if edit_non_food_form.is_valid(): # Cleaned_data item.input_type = 'non_food' quantity = edit_non_food_form.cleaned_data['quantity'] price_per_item = edit_non_food_form.cleaned_data['price_per_item'] non_foodDict = {'quantity': quantity, 'price_per_item': price_per_item} item.non_foodData = pickle.dumps(non_foodDict) item.save() return HttpResponseRedirect(reverse('backup_app.views.items_listing')) else: context = { 'edit_food_form': EditfooditemForm(instance=item), 'edit_non_food_form': Edit_non_food_itemForm(instance=item) } return render(request, 'backup_app/items_listing.html', context)