首页 > 解决方案 > 如何从 Django Admin 的 response_change 中获取自定义表单字段值?

问题描述

我通过覆盖向模型添加了自定义功能change_form.html。基本上,如果管理员批准了这些更改,我会让用户更改模型的对象。我添加了两个按钮,命名为accept-suggestionanddecline-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...

这两个按钮都会向用户发送一封电子邮件,说明该建议是否被拒绝或批准。到目前为止,一切都很好。问题是我想给管理员增加一个可能性,写一个简短的理由来解释为什么这个建议被拒绝了。于是我又变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>

这是最好的方法吗?<textarea>如果是这样,我怎样才能从内部获得上述值response_change?如果没有,你有什么建议?

非常感谢!

标签: djangodjango-modelsdjango-admindjango-modeladmin

解决方案


如果您添加一个name,您<textarea>将能够在服务器端检索内容。没有 a name,数据不会被发送到服务器(Django)。

所以是这样的:

<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>

应该允许您使用request.POST["decline-reasons"].


推荐阅读