views:

830

answers:

4

I'm wondering what a controller - such as the following Rails controller - would look like in Django, to perform an ajax update of the page after an asynchronous form submission (assuming the following is correct):

def create
@omelet = Omelet.new(params[:omelet])
render :update do |page|
  if @omelet.save
    page.replace_html 'notice', 'Omelet was successfully cooked'
  else
    page.replace_html 'notice', 'Sorry - the omelet could not be cooked'
  end
  page.replace_html 'omelets', :partial => 'meals/omelet_list',
    :locals => {:omelets =>  @omelet.meals.omelets }
end

end

I understand that Django does not have the same built-in ajax libraries. Does this mean something like the above would be much harder/more verbose in Django?

The "replace_html" calls really make things easy in Rails - I'm hoping there is an easy equivalent in Django.

Also, does Django have the notion of 'partials' - as Rails does?

A: 

I don't of anything in Django where you control specific parts of the page via the view functions (server side code).

The closest thing you can do in the view function is return plain text instead of html (or, return data in JSON format), then you have to use Javascript in the template file (which displays html) that sends the request and upon receiving the response, decides what parts of the DOM to manipulate (or what other things to do)

UPDATE

If you're used to program in this way, you might consider writing a little javascript framework that handles typical tasks like this,

(actually I'm starting to like the idea, might implement it myself!!)

hasen j
+3  A: 

The "page.replace_html" in Rails is really a manifestation of RJS - that is Javascript that is generated by the server (and the correct MIME type is sent, etc) and then the client takes that chunk of Javascript and inserts it somewhere in the DOM (probably replacing some other element as specified by DOM ID). So it really takes 2 coordinations: the server-side to send the Javascript back to the client and then the client to receive and act upon the received data.

I dont know if Django supports this but you would be better off googling around for "rjs django" and the like, in that essentially you are looking for the RJS equivalent in Django.

Cody Caughlan
+2  A: 

Rails likes to abstract client-side implementation details and tries to make this appear to be a server-side concern. Django assumes you already have your own client-side AJAX techniques and simply facilitates any server-side support that you need.

Different philosophies (Django never tries to write your HTML or Javascript for you except in the most trivial of cases).

The typical solution would be a bit of JQuery or whatever to make a normal HTTP request and then slot the HTML or JSON reponse into the DOM as required (this is usually a one liner in JQuery).

Just use normal Django views and templates to create your response.

andybak
+1  A: 

Currently Django is a server-side framework only. So you will need to use some Javascript framework to help you with this task.

Frameworks such as JQuery have an .load() method where you can just insert HTML into DOM, you could grab whatever response, a string message or complex HTML from a Django view and embed it in your page.

------ HTML ------
<div id="message"></div>
<input class="button" value="Get message"/>

------ Javascript -------
$("input").click(function(){
  $("#message").load("/get_message/");
});

------ Django view ------
def get_message(request):
  return HttpResponse("Your message")

Short answer, there isn't an equivalent, you'd have to write your own.

Jj