I have an included object's form:
<form method="post" class="object_form" id="event-core-form" action="{% url save_event_core_data event.id %}" enctype="multipart/form-data">
{{ form.as_p }}
<p>
<input class="object-submit" id="object-data-save" type="submit" value="Save data">
</p>
</form>
After hitting 'submit' button I'm running this script, which submits my form via ajax, updates data and should return updated form that will be inserted back in it's place:
$("#object-data-save").livequery("click", function(e) {
e.preventDefault();
$(this).parents("form:first").ajaxSubmit({
data: {"action": action},
"success": function(data) {
data = JSON.parse(data);
$("#core-data").html(data["html"]);
$("#message").show();
$("#message").fadeIn(400).html('<span>'+data["message"]+'</span>');
setTimeout(function(){
$("#message").fadeOut("slow", function () {
$("#message").hide();
});
}, 1500);
}
});
return false;
});
and this runs the following function :
def event_core_data(request, event_id):
template_name="management/core_event.html"
event = Event.objects.get(pk=event_id)
form = EventForm()
if request.method == "POST":
form = EventForm(instance=event, data=request.POST)
message = _("Error saving data")
if form.is_valid():
form.save()
message = _(u"Data saved.")
html = render_to_string(template_name, RequestContext(request, {
"form" : form,
"event" : event,
}))
result = simplejson.dumps({
"html" : html,
"message" : message,
}, cls = LazyEncoder)
result = HttpResponse(result)
logging.debug(result)
else:
form = EventForm(instance=event)
result = ""
try:
result = render_to_string(template_name, RequestContext(request, {
"form" : form,
"event" : event,
}))
except:
pass
return result
After saving it once everything works as expected. But after third update my form is not inserted in the parent template. Instead I'm redirected to the edit function's url and the form is rendered as a raw html. Also, I've noticed in firebug, that when I'm being redirected - no POST is sent and a dummy 'alert' in my javascript is not fired. This is the function displaying initial state (if it's of any help):
def manage_event(request, event_id,):
template_name = 'management/edit_event.html'
try:
event = Event.objects.get(pk=event_id)
except DoesNotExist:
url = reverse("manage_events")
return HttpResponseRedirect(url)
return render_to_response(template_name, RequestContext(request, {
"core_data" : event_core_data(request, event_id),
"event" : event,
}))
EDIT
Here is a test link to this project, where you can see what's going on. 'event_core_data' returns the request.POST to console upon successfull update.
http://ntt.vipserv.org/manage/events/2
I'm also wondering why my date picker widgets disappear after submission. Are those things somehow connected together ?
EDIT 2
I've already tried using .post or .ajax instead of .ajaxSubmit but without any luck.