Django question: Having a problem with saving
Hello I have an edit item form. From this view, look at item.current_item_status_date
.
def edit_item(request, client_id = 0, item_id = 0):
client = None
item = None
status = None
contact = None
status_id = request.POST.get('status_id', None)
contact_id = request.POST.get('contact_id', None)
save_item = request.POST.get('save_item', None)
save_status = request.POST.get('save_status', None)
try:
client = models.Client.objects.get(pk = client_id)
item = models.StorageItem.objects.get(pk = item_id)
except:
return HttpResponse开发者_JS百科NotFound()
try:
status = models.Status.objects.get(pk = status_id)
contact = models.Contact.objects.get(pk = contact_id)
except:
pass
if request.method == 'POST':
form = forms.ItemForm(request.POST, instance = item)
if form.is_valid() and save_item is not None:
item.current_item_status_date = date.today()
item.save()
form.save(True)
request.user.message_set.create(message = "Item {0} has been updated successfully.".format(item.tiptop_id))
return HttpResponse("<script language=\"javascript\" type=\"text/javascript\">window.opener.location = window.opener.location; window.close();</script>")
if status is not None and contact is not None and save_status is not None:
current_status = models.ItemStatusHistory(item = item, contact = contact, status = status,
user = request.user)
current_status.save()
request.user.message_set.create(message = "Item status has been updated successfully.")
else:
form = forms.ItemForm(instance = item)
title = str(client) + ' : Edit Item'
status_list = models.Status.objects.all()
return render_to_response('edit_item.html', {'form':form, 'title':title, 'status_list':status_list, 'item':item}, context_instance = RequestContext(request))
current_item_status_date saves todays date when the form is submitted. However, there is a small problem. In my form, I only want it to save when the button called new status is selected. Right now what my edit item form is doing is it's saving when the Save button is selected. I want my form to save this date only when new status is selected - not Save.
Here is what I have in my edit order template.
<td><input type="submit" name="save_status" value="new status"></td>
<input type="submit" name="save_item" value="Save" onclick="validate_item(this.form)">
Edit:: I have done these changes.
#code
if request.method == 'POST':
form = forms.ItemForm(request.POST, instance = item)
if form.is_valid() and save_item is not None:
form.save(True)
request.user.message_set.create(message = "Item {0} has been updated successfully.".format(item.tiptop_id))
return HttpResponse("<script language=\"javascript\" type=\"text/javascript\">window.opener.location = window.opener.location; window.close();</script>")
if request.POST.get('save_status'):
item.current_item_status_date = date.today()
item.save()
#code
Now, by doing this, the current item status date does get saved. But only if I close the form (It is a form that opens a new window). It does not get saved when I select status date and then click save. I think it is not getting saved properly into the the current_item_status_date field, rather than the database. I can tell it is not working when I click on save status, the field current_item_status does not update. It only updates when I close the form and open it up again. The only way it will work is if the form reloads getting information from the database.
This is what I am looking for:
- Clicking on status date should save the date.
- Clicking on status date and then clicking on save should save the date.
- Just clicking on save should not save the status date.
You are checking specifically for save_item. Change it to save_status and you should be fine if I understand your question.
if form.is_valid() and save_status is not None:
Removing the current_item_status_date
field from form = forms.ItemForm
seems to have worked. This way it can get saved properly.
精彩评论