def reply_news_article(request, news_article_pk): news_article = get_object_or_404(NewsArticle, pk=news_article_pk) if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.news_article = news_article post.created_by = request.user post.save() return redirect('news_posts', news_article_pk=news_article_pk) else: form = PostForm() return render(request, 'reply_news_article.html', {'news_article': news_article, 'form': form})
def news_posts(request, news_article_pk): analysis_objects = AnalysisArticle.objects.all() news_article = get_object_or_404(NewsArticle, pk=news_article_pk) if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.news_article = news_article post.created_by = request.user post.save() else: form = PostForm() context = { 'analysis_objects': analysis_objects, 'news_article': news_article, 'form': form } return render(request, 'news_post.html', context)
def addpost(request): post_form = PostForm args = {} args.update(csrf(request)) args['form'] = post_form if request.POST: form = PostForm(request.POST) #print('\n',form,'\n') if form.is_valid(): #Добавляем массив шаблона "Пост" в таблицу Статьи post = form.save(commit=False) post.author = request.user post.date = timezone.now() form.save() return redirect('/') else: error = 'У вас невалидная форма поста' render(request, 'main/error.html', {'error': error}) else: error = 'У вас не отправились данные на сервер' render(request, 'main/error.html', {'error': error}) return render(request, 'news/addpost.html', args)