Нужно в
class PostCreateView(LoginRequiredMixin, CreateView):
добавить
def add_post(request):
print (PostCreateForm)
form = PostCreateForm(request.POST or None)
# print (form)
context = {'form': form}
if request.method == 'POST' and form.is_valid():
post = form.save(commit=False)
formset = FileFormset(request.POST, files=request.FILES, instance=post) # 今回はファイルなのでrequest.FILESが必要
if formset.is_valid():
post.save()
formset.save()
return redirect('app:index')
# エラーメッセージつきのformsetをテンプレートへ渡すため、contextに格納
else:
context['formset'] = formset
# GETのとき
else:
# 空のformsetをテンプレートへ渡す
context['formset'] = FileFormset()
return render(request, 'app/post_form.html', context)
Ума не приложу как сделать
forms.py
from django import forms
from .models import Post, File
class PostCreateForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.fields.values():
field.widget.attrs['class'] = 'form-control'
class Meta:
model = Post
fields = '__all__'
FileFormset = forms.inlineformset_factory(
Post, File, fields='__all__',
extra=5, max_num=5, can_delete=False
)