使用django的ModelFormMixin的正确方法是什么?

时间:2022-10-22 11:09:54

I've read through the docs on this and yet I can't seem to get django's ModelFormMixin working properly. This is what I have in urls.py:

我已经阅读了有关这方面的文档,但我似乎无法让django的ModelFormMixin正常工作。这就是我在urls.py中所拥有的:

...
url(r'^vendors/edit/(?P<pk>\d+)/$', 'vendor_edit', name='vendor_edit'),
...

and in views.py:

并在views.py中:

class VendorEditView(DetailView, ModelFormMixin):
    form_class = VendorForm
    model = Vendor
    success_url = reverse_lazy('vendor_list')
    template_name = 'vendor_edit.html'

and the template:

和模板:

<form action='.' method='post'>{% csrf_token %}
    {{ form }}
    <button>Save</button>
</form>

I've tried mixing it into different types of views besides DetailView (View, TemplateView, FormView) with no luck.

我尝试将它混合到除DetailView(View,TemplateView,FormView)之外的不同类型的视图中,但没有运气。

What I expect to happen is that when I go to /vendors/edit/1, a form is on the page with the object's details already filled in for editing. What I'm getting is either a blank form, no form, or a 405 HTTP response. Is there something obvious I'm missing here?

我期望发生的是当我进入/ vendors / edit / 1时,页面上有一个表单,其中已经填写了对象的详细信息以进行编辑。我得到的是一个空白表单,没有表单,或405 HTTP响应。有什么明显的东西我在这里不见了吗?

1 个解决方案

#1


1  

Figured it out: Needed to use django's UpdateView with the mixin to get it working.

想出来:需要使用django的UpdateView和mixin来使它工作。

Edit: Per comments, no need to mix it in to the UpdateView, as it's already included in the inheritance chain.

编辑:每条评论,无需将其混合到UpdateView中,因为它已经包含在继承链中。

#1


1  

Figured it out: Needed to use django's UpdateView with the mixin to get it working.

想出来:需要使用django的UpdateView和mixin来使它工作。

Edit: Per comments, no need to mix it in to the UpdateView, as it's already included in the inheritance chain.

编辑:每条评论,无需将其混合到UpdateView中,因为它已经包含在继承链中。