Je veux créer un mot de passe comme champ de mot de passe dans les vues.
models.py:
class User(models.Model):
username = models.CharField(max_length=100)
password = models.CharField(max_length=50)
forms.py:
class UserForm(ModelForm):
class Meta:
model = User
Utiliser le widget comme PasswordInput
from Django import forms
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
Vous devez créer un ModelForm
( docs ), qui a un champ qui utilise le widget PasswordInput
de la bibliothèque de formulaires.
Cela ressemblerait à ceci:
from Django import models
class User(models.Model):
username = models.CharField(max_length=100)
password = models.CharField(max_length=50)
from Django import forms
class UserForm(forms.ModelForm):
class Meta:
model = User
widgets = {
'password': forms.PasswordInput(),
}
Pour plus d'informations sur l'utilisation des formulaires dans une vue, voir cette section de la documentation .
Voir mon code qui peut vous aider. models.py
from Django.db import models
class Customer(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField(max_length=100)
password = models.CharField(max_length=100)
instrument_purchase = models.CharField(max_length=100)
house_no = models.CharField(max_length=100)
address_line1 = models.CharField(max_length=100)
address_line2 = models.CharField(max_length=100)
telephone = models.CharField(max_length=100)
Zip_code = models.CharField(max_length=20)
state = models.CharField(max_length=100)
country = models.CharField(max_length=100)
def __str__(self):
return self.name
forms.py
from Django import forms
from models import *
class CustomerForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = Customer
fields = ('name', 'email', 'password', 'instrument_purchase', 'house_no', 'address_line1', 'address_line2', 'telephone', 'Zip_code', 'state', 'country')