def create_periodical(request, publisher_id): if request.method == 'POST': form = PeriodicalForm(request.POST) if form.is_valid(): periodical = form.save(commit=False) periodical.publisher = Publisher.objects.get(pk=publisher_id) periodical.save() periodical.save_categories(request.POST) return redirect('publication-show-periodical', publisher_id=publisher_id, periodical_id=periodical.id) else: form = PeriodicalForm() categories = Category.objects.all() return render(request, 'publication/periodical_form.html', {'form': form, 'categories': categories})
def update_periodical(request, publisher_id, periodical_id): periodical = get_object_or_404(Periodical, pk=periodical_id) if request.method == 'POST': form = PeriodicalForm(request.POST, instance=periodical) if form.is_valid(): periodical = form.save(commit=False) periodical.save() periodical.save_categories(request.POST) return redirect('publication-show-periodical', publisher_id=publisher_id, periodical_id=periodical_id) else: form = PeriodicalForm(instance=periodical) categories = Category.objects.all() periodical_categories = [] for category in periodical.categories.all(): periodical_categories.append(category.id) return render(request, 'publication/periodical_form.html', {'form': form, 'periodical_categories': periodical_categories, 'categories': categories, 'periodical_id': periodical_id})