You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
git-test/comments/views.py

48 lines
1.4 KiB

from django.shortcuts import render
# Create your views here.
from .models import Comment
from blog.models import Article
from .forms import CommentForm
from django.views.generic.edit import FormView
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from accounts.models import BlogUser
class CommentPostView(FormView):
form_class = CommentForm
template_name = 'blog/articledetail.html'
def get(self, request, *args, **kwargs):
article_id = self.kwargs['article_id']
url = reverse('blog:detail', kwargs={'article_id': article_id})
return HttpResponseRedirect(url)
def form_invalid(self, form):
article_id = self.kwargs['article_id']
article = Article.objects.get(pk=article_id)
return self.render_to_response({
'form': form,
'article': article
})
def form_valid(self, form):
"""提交的数据验证合法后的逻辑"""
article_id = self.kwargs['article_id']
article = Article.objects.get(pk=article_id)
if not self.request.user.is_authenticated():
pass
author_id = self.request.user.pk
comment = form.save(False)
comment.author = BlogUser.objects.get(pk=author_id)
comment.article = article
comment.save(True)
return HttpResponseRedirect('/')