2π
I think You can use the default class in django to a achieve the same result.
As far as i understood i got the scenario like this we have two django forms and we need it to be used in same template if that is the scenario we can use the LoginView from django.contrib.auth.views
which has several customizable option like you can give the additional form like this
class LoginUserView(LoginView):
authentication_form = LoginForm
extra_context = {"register_form": RegistrationForm}
template_name = 'accounts/index.html'
it will be using the get context data method to update the form which you will be able to get in the template and use accordingly .If you are not wishing to use the code like this you can still use it like this
class MyEmailUpdateView(LoginRequiredMixin, EmailView):
form_class = EmailForm
template_name = 'account/account_settings'
success_url = reverse_lazy('settings')
def get_context_data(self, **kwargs):
context = super(MyEmailUpdateView, self).get_context_data(**kwargs)
context['password_reset_form'] = ResetPasswordForm
return context
def form_valid(self, form):
return super(MyEmailUpdateView, self).form_valid(form)
Then you can handle your post in the form valid accordingly as your requirement.
Hope that helps get back if you need any additional requirement.
5π
You can use Django Multi Form View to combine many forms in view
After install you can use it like below:
class MultiFView(MultiFormView):
form_classes = {
'email_form' : AddEmailForm,
'change_password_form' : ChangePasswordForm
}
record_id = None
template_name = 'web/multi.html'
def forms_valid(self, forms):
email = forms['email_form'].save(commit=False)
email.save()
return super(MultiFView, self).forms_valid(forms)
and in template:
{{ forms.email_form.as_p }}
{{ forms.change_password_form.as_p }}
- [Django]-Python not recognising Django Installation. (ImportError: No module named django)
- [Django]-Django 1.6 + RabbitMQ 3.2.3 + Celery 3.1.9 β why does my celery worker die with: WorkerLostError: Worker exited prematurely: signal 11 (SIGSEGV)
1π
This can be solved by implementing a (kind of) partial update method for your view.
Tools:
First protect your sensitive data:
-
sensitive_variables
decorator:If a function (either a view or any regular callback) in your code uses local variables susceptible to contain sensitive information, you may prevent the values of those variables from being included in error reports using the sensitive_variables decorator
-
sensitive_post_parameters()
decorator:If one of your views receives an HttpRequest object with POST parameters susceptible to contain sensitive information, you may prevent the values of those parameters from being included in the error reports using the sensitive_post_parameters decorator
Create some code after:
-
Use
ModelForm
to create a form with specific fields of interest for your view. Donβt include thepassword
field, because we will add it in a different way to suit our needs:myapp/forms.py
:class PartialUpdateForm(forms.ModelForm): new_password = forms.CharField( required=False, widget=forms.widgets.PasswordInput ) class Meta: model = MyUser fields = ('email', 'other_fields',...)
Here you can use
exclude
instead offields
if you want to keep every other field except thepassword
: ex.exclude = ('password',)
-
Use an
UpdateView
to handle the hustle and override itβsform_valid
method to handle the partial update:myapp/views.py
:class MyUpdateView(UpdateView): template_name = 'account/account_settings' form_class = PartialUpdateForm success_url = reverse_lazy('settings') @sensitive_variables('new_password') @sensitive_post_parameters('new_password') def form_valid(self, form): clean = form.cleaned_data new_password = clean.get('new_password') if new_password: form.instance.password = hash_password(new_password) return super().form_valid(form)
-
Finally, create a url for that view:
myapp/urls.py
:... url(r'^your/pattern/$', MyUpdateView.as_view()),
This way you can handle:
- Email AND password update at the same time
- Only email update.
- Only password update.
Using code from this solution: Django: Only update fields that have been changed in UpdateView
- [Django]-Django Channels stops working with self.receive_lock.locked error
- [Django]-AttributeError: 'module' object has no attribute 'Datefield'
- [Django]-DRF APIView β How can I add help text to auto generated form?
- [Django]-Spam proof hit counter in Django
- [Django]-RawPostDataException: You cannot access body after reading from request's data stream