0

I am using a method on a model class to show a button on the changelist form. I have a very peculiar problem, Only the first button does not work. Even if I have 2 separate methods of buttons then the left will not work of the 1st row. I check out the source code of the page and the error is

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

On further inspection I found

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

this form to be open. Any suggestions to overcome this problem. Relevent codes are

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)

then using the two methods on list_display of admin. I am using Grappelli.

Shh
  • 944
  • 8
  • 16
  • It sounds like you are trying to render a form within a form (Which you can't do: http://stackoverflow.com/questions/379610/can-you-nest-html-forms). Where are you inserting/rendering that form code? – Timmy O'Mahony Jun 21 '13 at 10:14

2 Answers2

1

It looks like you simply want to provide a link in your admin change list to go to a view that will carry out an action on the row - you don't need a form to do this as you are simply doing a GET anyway with a link:

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

The reason you are having problems is that you can't nest a form within a form (the entire change list in the django admin is already a form)

Community
  • 1
  • 1
Timmy O'Mahony
  • 48,778
  • 16
  • 144
  • 168
0

If you simply want to preform an action on a row. Use actions: https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/

You can also make a field list_editable: https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_editable

And if you want to invent your own stuff (why use a framework?): Make a link with get parameters. Like Timmy O'Mahony suggested or point your link (without get parameters) to a custom model form.

allcaps
  • 9,782
  • 1
  • 28
  • 49
  • I am getting the error. ("tr input.action-select").actions(); Uncaught TypeError: Object [object Object] has no method 'actions'. Tried all the solutions but to no avail. – Shh Jun 22 '13 at 02:04