def valpost(request, slug = None):
instance = get_object_or_404(Post, slug=slug)
comments = Comment.objects.all()
form = CommentForm()
if request.method == 'POST'
form = CommentForm(request.POST)
if form.is_valid():
form.save()
return redirect(<success page>)
context = {
"title": instance.title,
"instance" : instance,
"form": form,
"comments" : comments,
}
template = 'post_detail.html'
return render(request, template, context)
from django.views.decorators.csrf import csrf_exempt
# Example
user = User.objects.get(pk=1) # or User.objects.create_user(...)
custom_user = CustomUser.objects.create(user=user, role=<your role>)
class ArticleDetailView(DetailView):
model = Article
template_name = "articles/article.html"
class ArticleDetailAddLikeView(ArticleDetailView):
def get_object(self):
object = super(ArticleDetailAddLikeView, self).get_object()
object.article_likes += 1
object.save()
return object