Skip to content

Instantly share code, notes, and snippets.

@agusrichard
Created May 16, 2022 22:11
Show Gist options
  • Save agusrichard/e6d2976caa518965636d39086c39cbd1 to your computer and use it in GitHub Desktop.
Save agusrichard/e6d2976caa518965636d39086c39cbd1 to your computer and use it in GitHub Desktop.
from django.contrib.auth.models import User
from django.forms import ModelForm, PasswordInput
from .models import Todo
class TodoForm(ModelForm):
class Meta:
model = Todo
fields = "__all__"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["title"].widget.attrs.update({"class": "form-control"})
self.fields["description"].widget.attrs.update({"class": "form-control"})
class UserForm(ModelForm):
class Meta:
model = User
fields = ["username", "password"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["password"].widget = PasswordInput()
self.fields["username"].widget.attrs.update({"class": "form-control"})
self.fields["password"].widget.attrs.update({"class": "form-control"})
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data["password"])
if commit:
user.save()
return user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment