【发布时间】:2017-05-03 09:07:07
【问题描述】:
我想将 Nation 模型 ID 传递给 Django 管理员 change_form,以便在编辑时显示一两个按钮。我尝试将 id 传递给 render_change_form 函数并在模板中完成,但出现以下错误。
'NationAdmin' object has no attribute 'nation_id'
N:B 这在编辑时工作正常,但当我想通过 Django 管理仪表板添加新模型对象时会抛出此错误。
NationAdmin 下的方法
#this method is to get the id
def change_view(self, request, nation_id=None, form_url='', extra_context=None):
self.nation_id = nation_id
return self.changeform_view(request, nation_id, form_url, extra_context)
#for form
def render_change_form(self, request, context, *args, **kwargs):
if self.nation_id: #django is telling me this line is causing error
nation= self.get_object(request, self.nation_id)
context.update({'nation':nation})
return super(NationAdmin, self).render_change_form(request, context, *args, **kwargs)
Change_form.html
{% if not nation.is_moderated and not nation.is_rejected %}
<a href="{% url 'activate_moderation' nation.id %}"> <input type="button" value="{% trans 'Approve' %}" name="_approvebutton" /></a>
{% endif %}
【问题讨论】:
标签: python django django-admin