Answer the question
In order to leave comments, you need to log in
How to validate form data if the form is generated by another View?
Wrote DetailView
which, in addition to the main work, generates a form:
class CategoryDetail(LoginRequiredMixin, DetailView):
model = Category
raise_exception = True
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['item_list'] = Item.objects.filter(
category_id=self.kwargs['pk']
).exclude(is_archived=True)
context['form'] = ItemChoiceForm
return context
class AddItemToCategory(LoginRequiredMixin, View):
raise_exception = True
def post(self, request, **kwargs):
item = Item.objects.filter(pk=request.POST['item'])
item.update(category=kwargs['pk'])
messages.success(request, 'Товар добавлен в категорию')
return redirect(reverse('category_detail_url', args=[kwargs['pk']]))
pk=request.POST['item']
. How in such a situation to check the data arrived from the client?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question