32

In Django / Pinax, I've come across the login form which starts like this :

<form class="login" method="POST" action="">

It works perfectly well. So I assume that either some java-script or something in the Django framework is putting the value into the action attribute.
So, my questions:

  • How does Django insert the action?
  • Why do they do it like this?
  • How can I find out what the action of this form is?

Update : I see this is not a Django thing at all, but what most browsers do.

interstar
  • 26,048
  • 36
  • 112
  • 180

4 Answers4

42

Having an action of an empty string in most browsers points the form at the URL the browser currently has loaded, which is the script that served the form in the first place.

Ry Biesemeyer
  • 1,484
  • 11
  • 10
  • ah. I didn't know that. So is this just laziness or is it good practice to rely on this – interstar Mar 13 '09 at 03:01
  • i'd say good practice, since you reduce the number of urls in your code. but I prefer not putting the action at all. does any browser out there need the empty action? – Javier Mar 13 '09 at 03:17
  • 15
    Forms without action attrbiute work fine in all mainstream browsers. However the action attribute is mandatory if you want your HTML to validate. – Imran Mar 13 '09 at 03:45
6

For an interesting insight on forms with empty actions read this thread, which gives you an updated HTML5-perspective on this matter.

Community
  • 1
  • 1
Aurelio
  • 24,702
  • 9
  • 60
  • 63
4

It's also possible that javascript loaded with this page could be setting an action once the page is loaded based on what application is using the page.

Another likely possibility is that the javascript is handling the onsubmit event. One might do that to prevent the page from reloading or redirecting to a specific page

jottos
  • 20,098
  • 9
  • 30
  • 27
0

I guess it's bit late to answer this post. Anyways, I'll what I learnt about this.

If the "action" is not specified in forms, then the Django looks up the HttpResponseRedirect in the corresponding view.

For example, in the example below:

if form.is_valid(): # All validation rules pass
        # Process the data in form.cleaned_data
        # ...
        return HttpResponseRedirect('/thanks/')

Once, the form is validated (and processed), the page is redirected to 'thanks'

CuriousCoder
  • 491
  • 5
  • 9