How to create Password Field in Model Django

后端 未结 4 928
名媛妹妹
名媛妹妹 2020-12-13 00:08

I want to create password as password field in views.

models.py:

class User(models.Model):
    username = models.CharField(max_lengt         


        
4条回答
  •  甜味超标
    2020-12-13 00:52

    You should create a ModelForm (docs), which has a field that uses the PasswordInput widget from the forms library.

    It would look like this:

    models.py

    from django import models
    class User(models.Model):
        username = models.CharField(max_length=100)
        password = models.CharField(max_length=50)
    

    forms.py (not views.py)

    from django import forms
    class UserForm(forms.ModelForm):
        class Meta:
            model = User
            widgets = {
            'password': forms.PasswordInput(),
        }
    

    For more about using forms in a view, see this section of the docs.

提交回复
热议问题