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', {})
👤jape
Source:stackexchange.com