【问题标题】:Multiple django forms in modal windows for a single view模态窗口中的多个 django 表单用于单个视图
【发布时间】:2018-05-23 16:58:31
【问题描述】:

我正在开发简单的费用跟踪应用程序。您可以在下面找到所有用户操作(费用或收入)的视图:

基于this thread我实现了bootstrap模态窗口来显示新的操作表单:

您可以在下面找到ManageOperations 视图,该视图负责显示上述视图:

class ManageOperations(ListView, FormView, OperationMixIn):

    model = Operation
    form_class = OperationForm
    template_name = "expenses/manage_operations.html"
    success_url = reverse_lazy('manage_operations')

    def get_context_data(self, **kwargs):
        context = super(ManageOperations, self).get_context_data(**kwargs)
        context['operations_list'] = Operation.objects.filter(user=self.request.user).order_by('-date')
        return context

    def get_form_kwargs(self):
        kwargs = super(ManageOperations, self).get_form_kwargs()
        kwargs.update(user=self.request.user,
                      initial={'account': Account.objects.get(user=self.request.user, default=True)})
        return kwargs

    def form_valid(self, form):
        operation = form.save(commit=False)
        operation.currency = Account.objects.get(pk=form.instance.account_id).currency
        self.update_account_balance(form)
        form.instance.user = self.request.user
        form.save()
        return super(ManageOperations, self).form_valid(form)

我想为“编辑”和“删除”操作实现相同的模式窗口。我认为 OperationDelete 视图会非常简单:

class OperationDelete(DeleteView, OperationMixIn):

    model = Operation
    success_url = reverse_lazy('manage_operations')

    def delete(self, *args, **kwargs):
        self.restore_account_balance(self.get_object().pk)
        return super(OperationDelete, self).delete(*args, **kwargs)

我可以将delete 方法移动到我的ManageOperations 视图并使其继承自DeleteView

在编辑现有操作时,事情变得更加复杂。目前以下代码负责处理现有条目的更新:

class OperationUpdate(UpdateView, OperationMixIn):

    model = Operation
    form_class = OperationForm
    success_url = reverse_lazy('manage_operations')

    def get_form_kwargs(self):
        kwargs = super(OperationUpdate, self).get_form_kwargs()
        kwargs.update({'user': self.request.user})
        return kwargs

    def form_valid(self, form):
        self.restore_account_balance(self.get_object().pk)
        self.update_account_balance(form)
        form.instance.user = self.request.user
        return super(OperationUpdate, self).form_valid(form)

如果我厌倦了将其合并到 ManageOperations 视图中,我将不得不处理 get_form_kwargsform_valid 方法的多个实现。

您能否告诉我我的方向是否正确,或者有更好、更优雅的方法来解决我的问题?创建一个大的ManageOperations 视图来负责所有与操作相关的操作对我来说似乎有点愚蠢。

【问题讨论】:

  • 前段时间,我遇到了 modalform to 的问题。为什么不使用函数,当事情变得复杂时,函数会更好。你也可以在.html 文件中创建一个没有表单的模型,并使用ajax 来调用你想要的函数。渲染这些模型表单的问题是它可以为视图中的每个项目渲染一个模式

标签: django twitter-bootstrap


【解决方案1】:

我相信您的方法很好,但我会创建 UpdateViewDeleteView AJAX 视图:让它们返回 JSON 而不是 HTML 模板,并使用模板中的 AJAX 调用它们。

  • 保持ManageOperations 原样
  • 在您的编辑模式表单中,让 AJAX 使用表单数据将其发布到您的 OperationUpdate 视图。
  • 更改您的 OperationUpdate 视图以返回 JSON 响应。您可以一直使用 Django REST 框架来解决这个问题,但是很容易调整现有的 Django 通用 CBV 以返回 JSON:覆盖 render_to_response()(如果表单无效)和 form_valid() 方法。您只需返回绑定的表单字段(值)和 JSON 中的错误。如果表单有效,则在 JSON 中返回保存的对象,以便 javascript 可以更新表中的相应值。
  • 更改您的 OperationDelete 视图,使其也返回 JSON 响应。
  • 在您的 Javascript 中添加对 JSON 响应的处理,以显示表单错误、关闭模式、更新表中的值等...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 2019-06-21
    相关资源
    最近更新 更多