0

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) 

1 Answer 1

1

Try something like this:

forms.py:

class ChangeForm(forms.ModelForm): class Meta: model = MyModel fields = ['field1', 'field2'] def __init__(self, *args, **kwargs): self.user = kwargs.pop('user') super(ChangeForm, self).__init__(*args, **kwargs) 

views.py:

# To render multiple forms, repeat the process def account_settings(request): change_form = ChangeForm(request.POST or None, instance=request.user, user=request.user) change_form_2 = ChangeForm2(request.POST or None) if request.method == 'POST': if change_form.is_valid(): change_form.save() if change_form_2.is_valid(): change_form_2.save() context = { 'change_form': change_form, 'change_form_2': change_form_2 } return render(request, 'change_form.html', context) 

That should give you the current data.

Note: Change the attributes to your needs. If you post your code, I can help you with that. However, I don't know what you're working with, but the above should be a good template to follow.

EDIT:

views.py:

@csrf_exempt def add_item(request): add_food_form = AddfooditemForm(request.POST or None) add_non_food_form = AddNonFooditemForm(request.POST or None) if request.method == 'POST': if add_food_form.is_valid(): 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) if add_non_food_form.is_valid(): 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() # This needs to be a url name return HttpResponseRedirect(reverse('url_name_here')) context = { 'add_food_form': add_food_form, 'add_non_food_form': add_non_food_form } # Make this its own template return render(request, 'backup_app/items_add.html', context) @csrf_exempt def edit_item(request, item_id): item = items.objects.get(id=item_id) edit_food_form = EditfooditemForm(request.POST or None, instance=item) edit_non_food_form = Edit_non_food_itemForm(request.POST or None, instance=item) if request.method == 'POST': if edit_food_form.is_valid(): 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) if edit_non_food_form.is_valid(): 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() # This needs to be a url name return HttpResponseRedirect(reverse('url_name_here')) else: context = { 'edit_food_form': EditfooditemForm(instance=item), 'edit_non_food_form': Edit_non_food_itemForm(instance=item) } # Make this its own template return render(request, 'backup_app/items_edit.html', context) def items_listing(request): # Any data you want to post about the listed items return render(request, 'backup_app/items_listing.html', {}) 
Sign up to request clarification or add additional context in comments.

14 Comments

I don't have user atrributes, just task objects and their associated ids. So would i need to include the part: "self.user = kwargs.pop('user')" ? Thanks for your help!
@shizznetz If you don't have user attributes, then no you shouldn't need it.
Am I not allowed to render to the same template twice? The edit form is not showing in the template I want it to.
@shizznetz I don't know how you're trying to render it, but it sounds like you only have one template on the view while trying to load it on two templates. Make a separate template variable with both templates, and then on the return make the 'change_form.html template instead. That will read multiple templates.
So there is no way for me to render multiple forms on a single template ? Thanks and sorry for my newbie questions!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.