27๐
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!
๐คWilliams
- Gunicorn: Can't connect to gunicorn.sock
- Pycharm Can't retrieve image ID from build stream
- How do I get AWS credentials in the AWS ECS docker container?
- A django model that subclasses an abc, gives a metaclass conflict
- Using height_field and width_field attribute in ImageField of Django
12๐
password = forms.CharField(
widget=forms.PasswordInput(attrs={'autofocus': 'autofocus'}))
for text input:
field = forms.CharField(
widget=forms.TextInput(attrs={'autofocus': 'autofocus'}))
๐คint_ua
- RuntimeError at / cannot cache function '__shear_dense': no locator available for file '/home/โฆsite-packages/librosa/util/utils.py'
- Jinja2 templates a superset of Django templates?
- Django on Google App Engine
- Database trouble in Django: can't reset because of dependencies
6๐
In html, all you need is autofocus
without arguments.
In the Django form, add autofocus as key with empty value:
search = forms.CharField(
label='Search for:',
max_length=50,
required=False,
widget=forms.TextInput(attrs={'autofocus': ''}))
๐คRamon
- How to count number of items in queryset without count()
- Image file not deleted when object with ImageField field is deleted
2๐
I just wanted to use the default Django login form, but add the autofocus attribute, so I derived a new form class from the default.
#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': ''})
Then I specified the new form class in 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'),
#...
)
๐คDon Kirkby
- What is difference between instance namespace and application namespace in django urls?
- Render an xml to a view
- What are the disadvantages of using AWS ELB directly with Gunicorn (no nginx)?
Source:stackexchange.com