У пользователя есть галерея, которая целиком редактируется в единой форме с профилем пользователя. У меня получилось сделать и вывести общую форму при помощи inlineformset_factory.
class CharCreateView(CreateView):
form_class = CharCreateForm
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(CharCreateView, self).dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
gallery_form = GalleryFormset()
return self.render_to_response(
self.get_context_data(
form=form,
gallery_form=gallery_form
)
)
def post(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
gallery_form = GalleryFormset(self.request.POST)
if form.is_valid() and gallery_form.is_valid():
return self.form_valid(form, gallery_form)
else:
return self.form_invalid(form, gallery_form)
def form_valid(self, form, gallery_form):
instance = form.save(commit=False)
char_id = self.object.id
gallery_form.instance = self.object
gallery_form.save()
return redirect(self.get_success_url(char_id))
def form_invalid(self, form, gallery_form):
return self.render_to_response(
self.get_context_data(
form=form,
gallery_form=gallery_form
)
)
def get_success_url(self, char_id=None):
return reverse('char:detail', args=[str(char_id)])
Но то же самое с UpdateView не работает никак, почему?