2013-06-21 74 views
0

我使用的模型类的方法来显示修改列表表单上的按钮。我有一个非常奇怪的问题,只有第一个按钮不起作用。即使我有两个单独的按钮方法,那么左边的第一行将不起作用。 我检查出页面的源代码和错误是Django管理使用按钮

Saw a form start tag but there was already an active form element. Nested tags are not allowed. Ignoring the tag. 

在进一步的检查,我发现

<form id="grp-changelist-form" action="" method="post"><input type='hidden' name='csrfmiddlewaretoken' value='l6Z2ez9F00XMVQjp0KIRIKgRIcQ9nnQc' /> 

这种形式是开放的。 任何建议来解决这个问题。 培训相关码是

Class MyModel(models.Model): 
. 
. 
    def method1(self): 
    return '<form action="path/to/action1" method="get"><input type="submit" value="%s"></form>' % (self.id, label) 

    def method2(self): 
    return '<form action="path/to/action2" method="get"><input type="submit" value="%s"></form>' % (self.id, label) 

然后使用上管理员的list_display的两种方法。 我正在使用Grappelli。

+0

这听起来像你正试图呈现一个表单中的表单(你不能这样做:http://stackoverflow.com/questions/379610/can-you-nest-html-forms)。你在哪里插入/渲染表单代码? –

回答

1

看起来你只是要提供你的管理变革列表的链接去,将开展该行的动作视图 - 你不需要一个形式要做到这一点,你只是做了与链接反正GET:

def method2(self): 
    return '<a href="path/to/action/%s?param1=%s" target="_blank">Do Something</a>' % (self.id, label) 

您有问题的原因是,you can't nest a form within a form(在Django管理整个变更列表已经是一种形式)

0

如果你只是想瓶坯在行动行。使用操作: https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/

你也可以做一个现场list_editable: https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_editable

如果你想创造你自己的东西(为什么要使用框架?): 请使用get参数的链接。就像Timmy O'Mahony建议或将您的链接(不带参数)指向自定义模型表单。

+0

我收到错误。 (“tr input.action-select”)。actions(); Uncaught TypeError:Object [object Object]没有方法“操作”。尝试了所有的解决方案,但无济于事。 – user1572215