django - 如何从 Django Admin 的 response_change 中获取自定义表单字段值?
问题描述
我通过覆盖向模型添加了自定义功能change_form.html
。基本上,如果管理员批准了这些更改,我会让用户更改模型的对象。我添加了两个按钮,命名为accept-suggestion
anddecline-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
?如果没有,你有什么建议?
非常感谢!
解决方案
如果您添加一个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"]
.
推荐阅读
- html - what's the purpose of adding ngModel attribute alone with no value?
- c# - 在 C# 中以窗口形式打开现有 PDF
- python - 使用异步数据加快加载时间(Google API 和 Kivy 的 UrlRequests)
- typescript - 检测到多个键盘按下,如何解决此打字稿问题绑定元素“键”隐含地具有“任何”类型.ts(7031)
- javascript - 在 Google 表格中调用对话框的正确方法是什么?
- javascript - 在js中使用XMLHttpRequest时的不同网格
- javascript - 我被这个方法问题困住了
- continuous-integration - bitbucket-pipelines.yml 文件中的部署环境“暂存”在管道中多次出现
- javascript - v-menu 在看到 v-list-item onclick 之前关闭
- laravel - livewire 类型提示的清洁解决方案