Override a Django generic class-based view widget -


say have basic createview form, this, allow new users register on site:

from django.contrib.auth import get_user_model django.http import httpresponse django.views.generic import createview  user = get_user_model()   class signup(createview):      model = user     fields = ['first_name', 'last_name', 'email', 'password'] 

i tried this, , found password field rendered in plain text; how go overriding view uses forms.passwordinput() instead? (i realise it's easiest define form hand, i'm curious how you'd that.)

you override get_form(), , modify form change widget on password field:

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 

but better way create custom form class. in custom class set widgets on meta class. this:

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 

usually put custom form class in forms.py file well.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -