Django 问题:保存时出现问题

发布于 2024-10-19 10:42:30 字数 3448 浏览 2 评论 0原文

您好,我有一个编辑项目表格。从此视图中查看 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 HttpResponseNotFound()
    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 保存提交表单时的今天日期。然而,有一个小问题。在我的表单中,我只希望当按钮名为新状态时保存 被选中。现在,我的编辑项目表单正在执行的操作是在选择保存按钮时进行保存。我希望我的表单仅在选择新状态时保存此日期,而不是保存

这是我的编辑订单模板中的内容。

<td><input type="submit" name="save_status" value="new status"></td>
<input type="submit" name="save_item" value="Save" onclick="validate_item(this.form)">

编辑::我已经完成了这些更改。

#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

现在,通过执行此操作,当前项目状态日期确实得到保存。但前提是我关闭表单(这是一个打开新窗口的表单)。当我选择状态日期然后单击“保存”时,它不会被保存。我认为它没有正确保存到 current_item_status_date 字段中,而不是数据库中。当我单击保存状态时,我可以看出它不起作用,字段 current_item_status 不会更新。仅当我关闭表单并再次打开它时它才会更新。它起作用的唯一方法是重新加载表单以从数据库获取信息。

这就是我正在寻找的:

  • 单击状态日期应该保存日期。
  • 单击状态日期,然后单击保存应保存日期。
  • 仅单击“保存”不应保存状态日期。

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 HttpResponseNotFound()
    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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心荒芜 2024-10-26 10:42:31

您正在专门检查 save_item。将其更改为 save_status 如果我理解你的问题,你应该没问题。

 if form.is_valid() and save_status is not None:

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:
浪漫之都 2024-10-26 10:42:31

form = forms.ItemForm 中删除 current_item_status_date 字段似乎有效。这样就可以正确保存了。

Removing the current_item_status_date field from form = forms.ItemForm seems to have worked. This way it can get saved properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文