2024-04-20 01:03:33 发布
网友
我想在视图中创建密码作为密码字段。
型号.py:
class User(models.Model): username = models.CharField(max_length=100) password = models.CharField(max_length=50)
表单.py:
class UserForm(ModelForm): class Meta: model = User
您应该创建一个ModelForm(docs),其中有一个字段使用表单库中的PasswordInput小部件。
ModelForm
PasswordInput
看起来是这样的:
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(), }
有关在视图中使用窗体的详细信息,请参见this section of the docs。
看看我的代码,可能会对你有帮助。 模型.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
表单.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')
使用小部件作为PasswordInput
from django import forms class UserForm(forms.ModelForm): password = forms.CharField(widget=forms.PasswordInput) class Meta: model = User
您应该创建一个
ModelForm
(docs),其中有一个字段使用表单库中的PasswordInput
小部件。看起来是这样的:
模型.py
forms.py(不是views.py)
有关在视图中使用窗体的详细信息,请参见this section of the docs。
看看我的代码,可能会对你有帮助。 模型.py
表单.py
使用小部件作为
PasswordInput
相关问题 更多 >
编程相关推荐