Modifying admin add behaviour

41 views
Skip to first unread message

Larry Martell

unread,
Apr 7, 2015, 5:41:04 PM4/7/15
to django...@googlegroups.com
I have a model and I want to have this behaviour in admin:

1. Only the add button is shown.
2. When the user clicks the add button, it brings up the standard
page, but with just 1 custom button shown.
3. When the user clicks that custom button, I execute an ajax call and
display the results below the add div, and this page is shown until
the user clicks a button.

I have all this coded, but I am facing the following problems:

1. I am not getting the response back from my ajax call. In the Chrome
developers tools, in the network tab for this request, in the
Status/Text column, it says "(cancelled)" - not sure what that means.
If I run the same request from my browser I do get a response. On the
server side I am getting 'error: (32, 'Broken pipe')' Googling that
revealed that it's because the browser has already moved on, so there
is nothing to receive the response. I've tried setting the timeout to
a large value, but no joy. How can I get it to wait for the response?

2. It immediately returns back to the main admin page - how can I get
it to stay on the add page until the users clicks a button?

3. After my code returns, a row is added to the database. How can I
prevent the row from being added?

This is my change_form.html:

{% extends "admin/change_form.html" %}

{% block submit_buttons_bottom %}
<div class="submit-row">
<input type="submit" value="Configure" class="default"
name="configure"
onclick="configureTools(document.getElementById('id_tool_configuration').value);"
/>
</div>

<script src="/static/scripts/jquery-1.7.js" type="text/javascript"></script>

<script type="text/javascript">
function configureTools(tcd) {
var toolConfigData = tcd;
$.ajax({
type : 'GET',
url: '{% url 'motor.configuration.views.configure' %}',
data: 'toolConfigData='+toolConfigData,
success: function(configOut) {
$('.submit-row').after(
$('<span />')
.html('<pre> Confguration succeded\n'+configOut+'</pre>')
);
},
error: function(configOut) {
$('.submit-row').after(
$('<span />')
.html('<pre> Confguration failed\n'+configOut+'</pre>')
);
}
});
};
</script>

{% endblock %}

Larry Martell

unread,
Apr 7, 2015, 7:05:40 PM4/7/15
to django...@googlegroups.com
On Tue, Apr 7, 2015 at 5:40 PM, Larry Martell <larry....@gmail.com> wrote:
> I have a model and I want to have this behaviour in admin:
>
> 1. Only the add button is shown.
> 2. When the user clicks the add button, it brings up the standard
> page, but with just 1 custom button shown.
> 3. When the user clicks that custom button, I execute an ajax call and
> display the results below the add div, and this page is shown until
> the user clicks a button.
>
> I have all this coded, but I am facing the following problems:
>
> 1. I am not getting the response back from my ajax call. In the Chrome
> developers tools, in the network tab for this request, in the
> Status/Text column, it says "(cancelled)" - not sure what that means.
> If I run the same request from my browser I do get a response. On the
> server side I am getting 'error: (32, 'Broken pipe')' Googling that
> revealed that it's because the browser has already moved on, so there
> is nothing to receive the response. I've tried setting the timeout to
> a large value, but no joy. How can I get it to wait for the response?

I solved the above issue - I was not returning false from the onclick
hander. Once i did that I started getting the response.


But I am still trying to solved the 2 issues below.

Larry Martell

unread,
Apr 8, 2015, 6:48:06 PM4/8/15
to django...@googlegroups.com
On Tue, Apr 7, 2015 at 7:04 PM, Larry Martell <larry....@gmail.com> wrote:
> On Tue, Apr 7, 2015 at 5:40 PM, Larry Martell <larry....@gmail.com> wrote:
>> I have a model and I want to have this behaviour in admin:
>>
>> 1. Only the add button is shown.
>> 2. When the user clicks the add button, it brings up the standard
>> page, but with just 1 custom button shown.
>> 3. When the user clicks that custom button, I execute an ajax call and
>> display the results below the add div, and this page is shown until
>> the user clicks a button.
>>
>> I have all this coded, but I am facing the following problems:
>>
>> 1. I am not getting the response back from my ajax call. In the Chrome
>> developers tools, in the network tab for this request, in the
>> Status/Text column, it says "(cancelled)" - not sure what that means.
>> If I run the same request from my browser I do get a response. On the
>> server side I am getting 'error: (32, 'Broken pipe')' Googling that
>> revealed that it's because the browser has already moved on, so there
>> is nothing to receive the response. I've tried setting the timeout to
>> a large value, but no joy. How can I get it to wait for the response?
>
> I solved the above issue - I was not returning false from the onclick
> hander. Once i did that I started getting the response.
>
>
> But I am still trying to solve the 2 issues below.
>
>> 2. It immediately returns back to the main admin page - how can I get
>> it to stay on the add page until the users clicks a button?

I solved this issue - I removed type="submit" from my custom button.
I then added another button they can click to return.

Now I'm left with just issue 3 - how to prevent the row from being
added to the database. Anyone know how to do that?
Reply all
Reply to author
Forward
0 new messages