简体   繁体   English

如何从 Django Admin 的 response_change 中获取自定义表单字段值?

[英]How can I get custom form field value from within Django Admin's response_change?

I've added a custom functionality to a model by overriding change_form.html .我通过覆盖change_form.html向模型添加了自定义功能。 Basically, I'm letting users change the objects of a model if these changes were approved by the admin.基本上,如果管理员批准了这些更改,我会让用户更改模型的对象。 I added two buttons, named accept-suggestion and decline-suggestion and I intend to handle the custom functionality through response_change method:我添加了两个按钮,命名为accept-suggestiondecline-suggestion ,我打算通过response_change方法处理自定义功能:

def response_change(self, request, obj):
    if "decline-suggestion" in request.POST:
        # do stuff...

    if "accept-suggestion" in request.POST:
        # do stuff...

Both buttons will send an e-mail to the user saying if the suggestion was declined or approaved.两个按钮都会向用户发送一封电子邮件,说明该建议是被拒绝还是被批准。 So far so good.到现在为止还挺好。 The problem is that I want to add the possibility to the admin write a brief justification explaining why the suggestion was declined.问题是我想增加管理员的可能性,写一个简短的理由解释为什么建议被拒绝。 So I changed change_form.html again.所以我又改了change_form.html

<div class="submit-row">
    <div class="float-left">
        <a class="decline-button-outlined accordion" type="button" href="#">DECLINE SUGGESTION</a>
    </div>
    <div class="float-right">
        <input class="accept-button" type="submit" name="accept-suggestion" value="ACEITAR SUGESTÃO">
    </div>
</div>

<div class="additional-infos">
    <fieldset class="module aligned">
        <div class="form-row">
            <label for="decline-reasons">Reasons for rejection:</label>
            <textarea
                placeholder="If you find necessary, provide information on the reasons that led to the rejection of the suggestion"
                id="decline-reasons" class="vLargeTextField" rows="5"></textarea>
        </div>
        <div class="submit-row">
            <div class="float-right">
                <input class="decline-button" type="submit" name="decline-suggestion" value="DECLINE">
            </div>
        </div>
    </fieldset>
</div>

Is this the best approach?这是最好的方法吗? If so, how can I get the value of the <textarea> above from within response_change ?如果是这样,我如何从response_change获取上述<textarea>的值? If not, what would you suggest?如果没有,你有什么建议?

Thank you very much!非常感谢!

If you add a name to your <textarea> you will be able to retrieve the contents on the server side.如果您向<textarea>添加name ,您将能够在服务器端检索内容。 Without a name , the data is not being sent to the server (Django).没有name ,数据不会发送到服务器(Django)。

So something like this:所以像这样:

<textarea
    placeholder="If you find necessary, provide information on the reasons that led to the rejection of the suggestion"
    id="decline-reasons" name="decline-reasons" class="vLargeTextField" rows="5"></textarea>

Should allow you to retrieve the text on the Django side with request.POST["decline-reasons"] .应该允许您使用request.POST["decline-reasons"]检索 Django 端的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 django 管理员中为 response_change 或 response_add 覆盖和调用 super - How to override and call super for response_change or response_add in django admin 在 Django Admin 中覆盖“response_change()”时如何删除无用的“UPDATE”查询? - How to remove a useless "UPDATE" query when overriding "response_change()" in Django Admin? Django ModelAdmin response_change 方法 - Django ModelAdmin response_change method Django admin - 如何在自定义管理表单中为多对多字段添加绿色加号 - Django admin - How can I add the green plus sign for Many-to-many Field in custom admin form 如何在保存之前更改 Django 表单字段值? - How can I change a Django form field value before saving? 如何覆盖 Django 管理更改表单中的字段值显示 - How to override field value display in Django admin change form 如何更改django admin中显示的自定义模型字段的值 - How to change the value of custom model field displayed in django admin 如何根据另一个字段的查找在 django admin 中设置字段值? - How can I set the field value in django admin based in another field's lookup? 如何在django admin中创建高级自定义搜索表单并使用django管理员更改列表显示 - how can i create advanced custom search form in django admin and use django admins change list display 从其他形式的Django管理员获取字段的值 - get value of field from other form django admin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM