覆盖Django基于类的视图窗口小部件

时间:2014-12-05 17:41:49

标签: django

假设我有一个基本的CreateView表单,就像这样,允许新用户在网站上注册:

from django.contrib.auth import get_user_model
from django.http import HttpResponse
from django.views.generic import CreateView

User = get_user_model()


class Signup(CreateView):

    model = User
    fields = ['first_name', 'last_name', 'email', 'password']

我刚试过这个,发现密码字段是以纯文本呈现的;我将如何重写视图,以便它使用forms.PasswordInput()代替? (我意识到手工定义表格可能最简单,但我只是对你如何做到这一点感到好奇。)

2 个答案:

答案 0 :(得分:35)

您可以覆盖get_form(),并修改表单以更改密码字段中的小部件:

from django import forms

class Signup(CreateView):
    model = User
    fields = ['first_name', 'last_name', 'email', 'password']

    def get_form(self, form_class):
        form = super(Signup, self).get_form(form_class)
        form.fields['password'].widget = forms.PasswordInput()
        return form

但更好的方法是创建一个自定义表单类。在自定义类中,只需在widgets类上设置Meta即可。像这样:

from django import forms

class SignupForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'email', 'password']
        widgets = {
            'password': forms.PasswordInput()
        }

class Signup(CreateView):
    form_class = SignupForm
    model = User

通常你也会将自定义表单类放在forms.py文件中。

答案 1 :(得分:1)

不确定这是否影响了Django的早期版本,但在较新的版本中,get_form()在覆盖该方法时应具有默认的form_class=None

更新后的示例(Python 3,Django 2.2)为:

from django import forms

class Signup(CreateView):
    model = User
    fields = ['first_name', 'last_name', 'email', 'password']

    def get_form(self, form_class=None):
        form = super().get_form(form_class)
        form.fields['password'].widget = forms.PasswordInput()
        return form

https://docs.djangoproject.com/en/2.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin