English 中文(简体)
如何 3. 集延戈元件中的Char Field
原标题:How Set focus to CharField of a django form element

在我的标识页上使用Django的表格。 <代码>I 想把重点放在第一稿上 页: 1 我尝试使用Jquery如下,但确实没有结果。

forms.py:
from django import forms
class LoginForm(forms.Form):
    userName = forms.EmailField(max_length=25)     
    password = forms.CharField( widget=forms.PasswordInput, label="password" )

login.html

% block headextra %}
    <script type="text/javascript">
        $(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
            };  
        }); 
        </script>       
    {% endblock %}

{% block content %} 
<form method = "POST" action ="/login/">{% csrf_token %}
<table align ="center">
    {{ form.as_table }}
<tr><td></td></tr>
<tr><td><input type="submit" value="Submit"></td></tr>
</table>
</from>
最佳回答

<代码>$(“#id_username”>>

问题回答

The proper Django way of answering this question is as follows (as it doesn t depend on js being enabled):

from django import forms

class LoginForm(forms.Form):
    user_name = forms.EmailField(max_length=25)     
    password = forms.CharField( widget=forms.PasswordInput, label="password" )

    def __init__(self):
        self.fields[ user_name ].widget.attrs.update({ autofocus :  autofocus 
             required :  required ,  placeholder :  User Name })
        self.fields[ password ].widget.attrs.update({
             required :  required ,  placeholder :  Password })

Also, for the record, we avoid the use of camelcase for object attributes. Cheers!

    password = forms.CharField(
        widget=forms.PasswordInput(attrs={ autofocus :  autofocus }))

for text input:

    field = forms.CharField(
        widget=forms.TextInput(attrs={ autofocus :  autofocus }))

在html中,你们都需要的是autofocus,没有论据。

在Django表格中,增加汽车作为具有空闲价值的钥匙:

search = forms.CharField(
                label= Search for: ,
                max_length=50,
                required=False,
                widget=forms.TextInput(attrs={ autofocus :   }))

我只想使用拖欠的Django标志,但添加了汽车保单,因此,我从违约中得出了新的表格。

#myapp/forms.py
from django.contrib.auth.forms import AuthenticationForm

class LoginForm(AuthenticationForm):
    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)
        self.fields[ username ].widget.attrs.update({ autofocus :   })

然后,我在<代码>urls.py上指明了新形式类别:

from myapp.forms import LoginForm

urlpatterns = patterns(
      ,
    url(r ^login/$ ,  django.contrib.auth.views.login ,
        {"template_name": "myapp/login.html",
         "authentication_form": LoginForm,
         "current_app": "myapp"}, name= login ),
    #...
    )




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签