Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改表单中的ID属性错误Django Admin_Python_Django_Django Admin - Fatal编程技术网

Python 更改表单中的ID属性错误Django Admin

Python 更改表单中的ID属性错误Django Admin,python,django,django-admin,Python,Django,Django Admin,我想将国家型号id传递给Django admin更改表单,以便在编辑时显示一两个按钮。我尝试将id传递给render_change_form函数,并在模板中完成,但我得到以下错误 'NationAdmin' object has no attribute 'nation_id' N:B这在编辑时工作正常,但当我想通过Django管理仪表板添加新的模型对象时,它会抛出这个错误 国家行政管理下的方法 #this method is to get the id def change_vie

我想将
国家
型号id传递给Django admin
更改表单
,以便在编辑时显示一两个按钮。我尝试将id传递给render_change_form函数,并在模板中完成,但我得到以下错误

'NationAdmin' object has no attribute 'nation_id'
N:B这在编辑时工作正常,但当我想通过Django管理仪表板添加新的模型对象时,它会抛出这个错误

国家行政管理下的方法

  #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 %}
{%if not nation.is\u主持而not nation.is\u拒绝%}
{%endif%}

您可以使用
原版
。无需设置
nation\u id

 def render_change_form(self, request, context, *args, **kwargs):
        if context.get('original'): #
             obj = context['original']
             #this will be the object you are trying to update
        return super(NationAdmin, self).render_change_form(request, context, *args, **kwargs)

可以“原版”从哪里来?模型管理中的关键字?它是在管理中使用的关键字,将是您正在更改的对象。如果为“添加新对象”,则为“无”。这就是为什么我把它放在这里,好吧,让我试试。谢谢试过了。我得到了模板的反向匹配错误。所以我把它改为:def render_change_form(self,request,obj,context,*args,**kwargs):if context.get('original'):obj=context['original']return super(NationAdmin,self)。render_change_form(request,obj,context,*args,**kwargs)和get TypeError:render_change_form()关键字参数“obj”有多个值,您认为缺少什么?它不取决于上述答案。它将是
“{%url'activate\u moderation'nation.id%}”
。您需要解决这个问题