简体   繁体   English

如何获取django的密码文本框?

[英]How can I get password text box for django?

class Users(models.Model):
    username = models.CharField(max_length=255)
    password = models.CharField(max_length=300)
    password_token = models.CharField(max_length=300, default='0')

The password text input field is regular text input field. 密码文本输入字段是常规文本输入字段。 It's not password field. 这不是密码字段。

How can I get password field in django? 如何在Django中获取密码字段?

I tried password = models.CharField(max_length=300, widget=forms.PasswordInput ) but got error. 我尝试了password = models.CharField(max_length=300, widget=forms.PasswordInput )但是遇到了错误。

Widgets are assigned in the forms, not on the model. 小部件是在表单中分配的,而不是在模型上分配的。

Please see the docs for forms documentation: https://docs.djangoproject.com/en/dev/ref/forms/widgets/ 请参阅有关表单文档的文档: https : //docs.djangoproject.com/en/dev/ref/forms/widgets/

One way if you have a forms.py in your app is to specify the widgets in your class Meta eg 如果您的应用程序中有一个forms.py,则一种方法是在类Meta中指定小部件,例如

from django.forms import ModelForm, PasswordInput
class UserForm(ModelForm):

    class Meta:
        model = Users

        widgets = {
            'password' : PasswordInput(),
        }

You can use this approach. 您可以使用这种方法。 Create a file named forms.py inside your app. 在您的应用程序内创建一个名为forms.py的文件。

for example: 例如:

from django import forms
from models import Users

class UsersForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = Users

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM