def add_article(request): if request.method == 'POST': form = ArticleForm(request.POST, request.FILES) if form.is_valid(): form = form.save(commit=False) volunteer = get_object_or_404(Volunteer, user=request.user) form.author = volunteer form.save() return HttpResponseRedirect(reverse('ArticlesList', args=[])) else: form = ArticleForm() template_name = 'blog/new_article.html' context = { 'form': form, } return render(request, template_name, context)
def update_article(request, pk): article = get_object_or_404(Article, pk=pk) if request.method == 'POST': form = ArticleForm(request.POST, request.FILES, instance=article) if form.is_valid(): form = form.save(commit=False) volunteer = get_object_or_404(Volunteer, user=request.user) form.author = volunteer form.save() return HttpResponseRedirect( reverse('ArticleDetail', kwargs={ 'pk': pk, })) else: form = ArticleForm(instance=article) template_name = 'blog/update_article.html' context = { 'form': form, } return render(request, template_name, context)