Ohh thanks that helped a lot.
Now in one view I still have a problem.
It uses the a formset and tries to give that back while overwriting the 
get_form function:

views.py
class LayoutFormView(LoginRequiredMixin, StaffuserRequiredMixin, UpdateView
):
    template_name = 'abc/layout.html'
    form_class = LayoutFormSet
    breadcrumbs = ['config_list']
    
    def form_valid(self, form):
        if 'save' in self.request.POST.keys():
            layout_change_settings(form=form, **kwargs)
            return HttpResponseRedirect(self.get_success_url())
            
        elif 'reset_to_default' in self.request.POST.keys():
            layout_reset_to_default(form=form, **kwargs)
            return HttpResponseRedirect(reverse('update_layout', kwargs={
'pk': self.kwargs['pk']}))

    def get_object(self, queryset=None):
        return LayoutSet.objects.get(settings__pk=self.kwargs['pk'])
        
    def get_context_data(self, **kwargs):
        context = super(LayoutFormView, self).get_context_data(**kwargs)
        
        if self.object.label not in ['default']:
            context['label'] = "custom"
        else:
            context['label'] = self.object.label

        return context
    
    def get_success_url(self):
        return reverse('update_layout', kwargs={'pk': self.kwargs['pk']})
    
    def get_form(self, form_class=None):
        if self.request.POST:
            return form_class(self.request.POST)
        else:
            initial = [{'param': 'abc',
                        'choosen': 'None'}]
            return form_class(initial=initial)

forms.py
class LayoutForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(LayoutForm, self).__init__(*args, **kwargs)
    
    param = forms.CharField()
    choosen = forms.BooleanField()
    
    class Meta:
        fields = ['choosen']
    
    def is_valid(self):
        return True

LayoutFormSet = formset_factory(LayoutForm, extra=0)


According to my understanding this does not work, because a formset is 
assigned to the form_class attribute.
It is not possible to determine the form in the base get_form, because a 
formset is assigned.

I would add the formset in the context and then use it in the template. I 
still seem to have a problem after saving, but I guess this is releated 
with the improper use of the formset.

Thanks for hints.
schaf



Am Mittwoch, 25. Januar 2017 12:19:55 UTC+1 schrieb Melvyn Sopacua:
>
> On Tuesday 24 January 2017 05:07:25 scha...@gmail.com <javascript:> wrote:
>
> > sorry I wanted to shorten the code before, to not post too much.
>
> > Here the code:
>
> > 
>
> > views.py
>
> > class MFormsView(LoginRequiredMixin, StaffuserRequiredMixin,
>
> > UpdateView, MFView):
>
>  
>
> And there you have it:
>
> MFormsView uses UpdateView 
> <https://ccbv.co.uk/projects/Django/1.10/django.views.generic.edit/UpdateView/>
>  
> which pulls in ModelFormMixin. So this view needs a fields attribute:
>
>  
>
> class M MFormsView(LoginRequiredMixin, StaffuserRequiredMixin, UpdateView, 
> MFView):
>
> fields = ['foo', 'bar', 'baz']
>
>  
>
>  
>
> -- 
>
> Melvyn Sopacua
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/95dc47c1-432d-4181-b605-f714159ae278%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to