Converting a function-based view to a class-based view only with and without a model (object)

Right now, the password in the user profile is changing this way. What is the best way to convert this view into a class, knowing that it does not have a model?

This is a view for changing your password.

@login_required def profile_change_password(request): """ Change password of user. """ user = get_object_or_404(User, username__iexact=request.user.username) if request.method == 'POST': form = PasswordChangeFormPrivate(user=user, data=request.POST) if form.is_valid(): form.save() messages.add_message (request, messages.INFO, _('password changed')) return HttpResponseRedirect(reverse('profile_view_details')) else: form = PasswordChangeFormPrivate(user=request.user) return render_to_response('profiles/profile_change_password.html', { 'form': form,}, context_instance=RequestContext(request) ) 

This is the password reset form.

 class PasswordChangeFormPrivate(PasswordChangeForm): def __init__(self, *args, **kwargs): super(PasswordChangeForm, self).__init__(*args, **kwargs) def clean_new_password2(self): password1 = self.cleaned_data.get('new_password1') password2 = self.cleaned_data.get('new_password2') if password1 and password2: if password1 != password2: raise forms.ValidationError(_("The two password fields didn't match.")) min_len = getattr(settings, "PASSWORD_MINIMUM_LENGHT", 6) if len(password1) < min_len: raise forms.ValidationError(_("Password too short! minimum length is ")+" [%d]" % min_len) return password2 

This is the URL

 url(r'^password/change/$', profile_change_password, name='profile_change_password' ), 

As you can see, no model is involved, since the password will replace the "User" password field with validation. Any easy way to convert this to a class based view? Does it matter?

+4
source share
1 answer

It is not necessary to be involved in the model - you can use FormView . It will look something like this:

 from django.core.urlresolvers import reverse from django.utils.decorators import method_decorator from django.contrib.auth.decorators import login_required from django.views.generic.edit import FormView from myapp.forms import PasswordChangeFormPrivate class ProfileChangePassword(FormView): form_class = PasswordChangeFormPrivate success_url = reverse('profile_view_details') template_name = 'profiles/profile_change_password.html' def get_form_kwargs(self): kwargs = super(ProfileChangePassword, self).get_form_kwargs() kwargs['user'] = self.request.user return kwargs def form_valid(self, form): form.save() messages.add_message(self.request, messages.INFO, _('profile changed')) return super(ProfileChangePassword, self).form_valid(form) @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(ProfileChangePassword, self).dispatch(*args, **kwargs) 

I'm not sure why you have

 user = get_object_or_404(User, username__iexact=request.user.username) 

In any case, you need a login for the form, so request.user guaranteed to be a valid user.

+4
source

Source: https://habr.com/ru/post/1393522/


All Articles