tags:

views:

137

answers:

4
+1  Q: 

Events in Django

Hi,

This might be a very dumb question but I can't find an answer anywhere. I was wondering how do you handle events in Django. I've read the tutorial in their webpage and I cant find a sign of explanation or I'm missing it.

I work in ASP.NET webforms and the way to do that, as you probably know, is putting the event name in the ASP.NET control and calling a particular procedure. I know Django is a very different framework because it's MVC, but I have taken a quick view to a ROR tutorial and I think I've seen they use something similar to ASP.NET webforms, if I've understood correctly, something like embedding ruby code in the html interface and declaring an action there, which I suppose calls the respective function, something like this <%= ..., action:'action1', ...%> ,which calls a function 'def action1:' in the controller, similar (only in that sense) to asp.net, but webforms doesnt render a view with the name of the function among other things.

So the question is simply that, how do you use events in Django. I hope I was at least a bit clear because it's difficult for me to ask these questions in english

A: 

Suppose I have a form with a button and 2 dropdownlists. I want the button to insert a record in a database for example, but I want a dropdownlist index change to call a query to filter another dropdownlist. How do I tell the form to do one thing when the form is submitted from the button and how do I tell the same form to do another thing when the form is submitted from the dropdownlist (in Django)

You can edit your original post instead of posting an Answer to add to the question.
Kai
Thanks. Anyway I cant delete this post now, can I?Did I make myself clear? The question might be very dumb but I dont get how it works that in Django
+2  A: 

You do this in the view, dependent on the values of the request.POST dictionary.

For example, say you have two buttons on the HTML form, one with name="submit" and one with name="add". Then, in your view, you can do:

if 'add' in request.POST:
    # do add action
elif 'submit' in request.POST:
    # do submit action

This is exactly how the admin app manages the difference between the "Save", "Save and continue", and "Save and add another" buttons.

Daniel Roseman
+2  A: 

This concept of control (TextBox, Button, etc.) and page events, in the way you describe, is not carried over to most other frameworks. This was all part of Microsoft's attempt to make the web development experience very similar to that of developing Windows applications -- for better or for worse.

For instance, in a WebForms application:

button.Click += Button_Click;

private void Button_Click(object sender, EventArgs e)
{
    // handle action here
}

Would roughly correspond to something like (client-side, in a template):

<form action="/myaction" method="POST">
    <input type="submit" value="Click me" />
</form>

Server side view:

def myaction(request):
    if request.method == "POST":
        # handle action here
        pass

In other words, what you want to do in Django and indeed in other MVC frameworks is have unique "actions", or methods, on your controller classes (or view functions in Django). These actions generally respond to HTTP GET and POST requests.

If you want client-side events on your controls, you'll want to look at using a JavaScript framework such as jQuery.

Ryan Duffield
+1 for code examples
T. Stone
+3  A: 

I'm a former ASP.NET developer who switched almost entirely to Django, so I know exactly what you're asking about.

Probably the simplest way I can explain it is this: What you're thinking of as an 'event' would be a URL AND View in Django. Abstract down what you were doing in ASP.NET and it'll make sense -- You had a control, which was just an element on a page. That was mapped to an event, which was really nothing more than a function. So the model of what you're doing looks like this:

object > method

In Django, it's similar, but there's an extra step. ASP.NET automatically connected the object to the method. In Django, you'll need to do that yourself. You do so by creating a URL for that object (urls.py), and then assigning that URL to a view (views.py) which is really nothing more than a method. In Django the same model looks like this:

object > url > method

In many ways they're achieving the same end effect -- something happens and it's handled by a method on the server. Django is simply a bit more open and lets you configure how it happens (and requires that you understand that and handle it yourself).

T. Stone