Issues regarding field types in Django

后端 未结 2 1758
长情又很酷
长情又很酷 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:34

    There is no PasswordField() in django. You can to use CharField() to store a password, but to show it as password input type in a html form you can specify a widget to it.

    Example:

    password = forms.CharField(widget=forms.PasswordInput())
    

    More reference at :PasswordInput

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题