title: How Set focus to CharField of a django form element
body: <p>Am using Django's form for my login page. <code>I want to set focus to the first textField (username) when the page loads.</code> I tried using Jquery as follows, But doen't get the result.</p>
  
  <pre><code>forms.py:
  from django import forms
  class LoginForm(forms.Form):
      userName = forms.EmailField(max_length=25)     
      password = forms.CharField( widget=forms.PasswordInput, label="password" )
  </code></pre>
  
  <p>Login.html</p>
  
  <pre><code>% block headextra %}
      &lt;script type="text/javascript"&gt;
          $(document).ready(function() {
               window.onload = function() {
               $("#id_username").focus();
               // alert('test') if I add this line its works
               //  setting focus to a textbox which added to template page direcltly using html tag the focus() method works well
              };  
          }); 
          &lt;/script&gt;       
      {% endblock %}
  
  {% block content %} 
  &lt;form method = "POST" action ="/login/"&gt;{% csrf_token %}
  &lt;table align ="center"&gt;
      {{ form.as_table }}
  &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;&lt;input type="submit" value="Submit"&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  &lt;/from&gt;
  </code></pre>