Created
May 16, 2022 22:11
-
-
Save agusrichard/e6d2976caa518965636d39086c39cbd1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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