dimanche 29 septembre 2019

Django - How to test this CBV?

:) I'm near the end of my first django "project". I managed to make tests for everything, but one view.

I'm not really sure how and what to test here, does someone mind helping me out ?

class CommentCreateView(CreateView):

    def get(self, request, *args, **kwargs):
        context = {'form': CommentForm()}
        return render(request, 'news/add_comment_to_article.html', context)

    def post(self, request, *args, **kwargs):
        form = CommentForm(request.POST)
        if form.is_valid():
            article = get_object_or_404(Article, pk=kwargs.get('pk'))
            print(article)
            comment = form.save(commit=False)
            comment.post = article
            comment.save()
            return HttpResponseRedirect(reverse('news:article', kwargs={'article_id': article.pk}))
        else:
            form = CommentForm()
            return render(request, 'news/add_comment_to_article.html', {'form': form})

Thanks for the help !!

Aucun commentaire:

Enregistrer un commentaire