Skip to content

Instantly share code, notes, and snippets.

@rafaelcanovas
Last active March 2, 2021 17:04
Show Gist options
  • Save rafaelcanovas/6116630 to your computer and use it in GitHub Desktop.
Save rafaelcanovas/6116630 to your computer and use it in GitHub Desktop.
Monkey-patching HTML5 required attribute for Django forms.
from django import forms
class HTML5Form(forms.Form):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
for _, field in self.fields.items():
if field.widget.is_required:
field.widget.attrs['required'] = 'required'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment