Issues regarding field types in Django

后端 未结 2 1785
长情又很酷
长情又很酷 2021-01-25 11:57

I am new to Django and I want to make a user registration form in Django. While creating model, I gave a fieldtype->PasswordField() to password field but when I run this model i

2条回答
  •  灰色年华
    2021-01-25 12:46

    Theory

    1. Use django.db.models.CharField, since this is an OK database column type for a password string

    2. Use django.forms.PasswordWidget, to represent this field in a form, see overriding default form field widgets

    Example

    models.py:

    from django.db import models
    
    
    class YourModel(models.Model):
        password = models.CharField(max_length=200)
    

    forms.py:

    from django import forms
    
    from models import YourModel
    
    
    class YourModelForm(forms.ModelForm):
        class Meta:
            widgets = {'password': forms.PasswordField}
            model = YourModel
    

提交回复
热议问题