Remove autofocus attribute from field in Django

django, python

Solution

(From a comment by @mariodev)

You should be able to do:

self.fields['username'].widget.attrs.pop("autofocus", None)

to remove the item from the `attrs` collection iff the specified item exists.

Problem

I'm working on a sign up form, I have a few custom fields before the username. What's happening is that by default the focus is on the username field and I can't remove the autofocus attribute from this field. I know I can work around using JavaScript but I'm trying to do this in the right way on Django. ``` from django import forms from django.contrib.auth.models import User from project.userprofile.models import UserProfile class UserSignupForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(UserSignupForm, self).__init__(*args, **kwargs) self.fields['username'].widget.attrs['autofocus'] = 'off' ``` Did I miss something? UPDATE The solution is: ``` self.fields['username'].widget.attrs.pop("autofocus", None) ``` Thanks @mariodev!

Original source