[英]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-suggestion
和decline-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.