本文整理汇总了Python中comments.forms.CommentForm.is_valid方法的典型用法代码示例。如果您正苦于以下问题:Python CommentForm.is_valid方法的具体用法?Python CommentForm.is_valid怎么用?Python CommentForm.is_valid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类comments.forms.CommentForm
的用法示例。
在下文中一共展示了CommentForm.is_valid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: NewsDetail
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
class NewsDetail(DetailView):
model = Article
template_name = 'news/article.html'
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
self.comment_form = CommentForm(request.POST or None) # create filled (POST) or empty form (initial GET)
if request.method == 'POST':
self.comment_form.instance.article_id = self.get_object().id
if self.comment_form.is_valid():
comment = self.comment_form.save(commit=True)
client = Client()
client.publish(self.get_object().get_cent_comments_channel_name(), comment.as_compact_dict())
cent_response = client.send()
print('sent to channel {}, got response from centrifugo: {}'.format(self.get_object().get_cent_comments_channel_name(),
cent_response))
return HttpResponse()
# return HttpResponseRedirect(reverse('news:detail', args=[self.get_object().pk,]))
else:
return super().get(request, *args, **kwargs)
# context = self.get_context_data()
# return render('news/article.html', context)
return super().dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['comment_form'] = self.comment_form
return context
示例2: post_show
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
def post_show(request, blog, slug):
try:
blog = Blog.objects.get(slug=blog)
except:
raise Http404(u'Não disponível')
try:
post = Post.published.get(slug=slug, blog=blog)
except:
raise Http404(u'Não disponível')
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.submit_date = datetime.now()
comment.user = None
comment.site = Site.objects.get_current()
comment.ip_address = request.META.get("REMOTE_ADDR", None)
comment.content_type = ContentType.objects.get_for_model(post)
comment.object_pk = post.id
comment.save()
return HttpResponseRedirect(reverse('post_show',args=(blog.slug,post.slug)))
else:
form = CommentForm()
return render_to_response('blog/post_show.html', {
'post': post,
'blog': blog,
'form':form,
}, context_instance=RequestContext(request))
示例3: new
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
def new(request, pk):
note = get_object_or_404(Note, pk=pk)
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.note = note
comment.user = request.user
comment.save()
# add rep
add_rep(request, c=comment)
# create notification
notify(comment=comment)
d = {
'comment': comment,
'comment_form': CommentForm(),
'note': note,
'reply_form': ReplyForm(),
'static': settings.STATIC_URL,
}
comment_form = loader.get_template('comments/comment_form.html')
comment_temp = loader.get_template('comments/comment.html')
context = RequestContext(request, add_csrf(request, d))
data = {
'comment': comment_temp.render(context),
'comment_count': note.comment_count(),
'comment_form': comment_form.render(context),
'comment_pk': comment.pk,
'note_pk': note.pk,
}
return HttpResponse(json.dumps(data), mimetype='application/json')
return HttpResponseRedirect(reverse('readings.views.detail',
args=[reading.slug]))
示例4: detail
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
def detail(request, pk):
post = get_object_or_404(Post, pk=pk)
if post.draft:
if post.user != request.user:
messages.error(request,'you have not permission to see that post',extra_tags='alert alert-danger')
return redirect('posts:index')
form=CommentForm(request.POST or None)
# add comment
if form.is_valid() and request.user.is_authenticated:
instance=form.save(commit=False)
instance.user=request.user
instance.post=post
parent=None
try:
parent_id=int(request.POST.get('parent_id'))
except:
parent_id=None
if parent_id:
query=Comment.objects.filter(id=parent_id)
if query.exists():
parent=query.first()
instance.parent=parent
instance.save()
return redirect('posts:detail',pk)
return render(request, 'detail.html', {'post': post,'comment_form':form})
示例5: post
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated():
return Response(status.HTTP_401_UNAUTHORIZED)
ct = ContentType.objects.get_for_model(MODELS_MAPPINGS[kwargs['model']])
try:
obj = ct.get_object_for_this_type(pk=kwargs['object_id'])
except ObjectDoesNotExist:
raise ErrorResponse(status.HTTP_404_NOT_FOUND)
form = CommentForm(request.POST)
if not form.is_valid():
raise ErrorResponse(status.HTTP_400_BAD_REQUEST)
data = {
'content_type': ct,
'object_id': kwargs['object_id'],
'user': request.user,
'created': datetime.datetime.now(),
'content': form.cleaned_data['content'],
}
parent = form.cleaned_data['parent']
if parent:
instance = parent.add_child(**data)
else:
instance = Comment.add_root(**data)
if request.is_ajax():
return Response(status.HTTP_201_CREATED)
return HttpResponseRedirect(obj.get_absolute_url())
示例6: AddCommentView
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
class AddCommentView(TemplateView):
@method_decorator(writeable_site_required)
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
commentable_id = self.args[0]
self.commentable = get_object_or_404(self.commentable_model, id=commentable_id)
self.comment = Comment(commentable=self.commentable, user=request.user)
return super(AddCommentView, self).dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
self.form = CommentForm(instance=self.comment, prefix='comment')
context = self.get_context_data()
return self.render_to_response(context)
def post(self, request, *args, **kwargs):
self.form = CommentForm(request.POST, instance=self.comment, prefix='comment')
if self.form.is_valid():
self.form.save()
return redirect(
self.commentable.get_absolute_url() + ('#comment-%d' % self.comment.id)
)
else:
context = self.get_context_data()
return self.render_to_response(context)
def get_context_data(self, **kwargs):
context = super(AddCommentView, self).get_context_data(**kwargs)
context['commentable'] = self.commentable
context['commentable_name'] = self.get_commentable_name(self.commentable)
context['comment_form'] = self.form
context['submit_action'] = self.submit_action
return context
template_name = 'comments/add_comment.html'
示例7: video_show
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
def video_show(request, slug):
try:
video = Video.objects.get(slug=slug)
except:
raise Http404(u'Vídeo não encontrado.')
related_videos = video.get_related_videos()
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.submit_date = datetime.now()
comment.user = None
comment.site = Site.objects.get_current()
comment.ip_address = request.META.get("REMOTE_ADDR", None)
comment.content_type = ContentType.objects.get_for_model(video)
comment.object_pk = video.id
comment.save()
return HttpResponseRedirect(reverse('video_show',args=[video.slug]))
else:
form = CommentForm()
return render_to_response('videos/video_show.html', {
'video': video,
'related_videos':related_videos,
'form':form,
}, context_instance=RequestContext(request))
示例8: edit_comment
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
def edit_comment(request, sub_id, comment_id):
submission = Submission.objects.get(pk=sub_id)
try:
comment = Comment.objects.get(pk=comment_id)
if comment.user.email != request.session['email']:
return HttpResponseRedirect('/submissions/show/'+str(sub_id))
except Comment.DoesNotExist:
raise Http404("Comment does not exist")
if request.method == 'POST':
comment_form = CommentForm(request.POST, instance=comment)
if comment_form.is_valid():
comment = comment_form.save(commit=False)
comment.user = UserProfile.objects.get(email=request.session['email'])
# comment.content = bleach.clean(comment.content, strip=True)
# comment.content = markdown.markdown(comment.content)
comment.save()
submission.comments.add(comment)
return HttpResponseRedirect('/submissions/show/' + str(sub_id))
else:
raise Http404("Form is not valid")
elif request.method == 'GET':
c_edit = comment
# c_edit.content = bleach.clean(c_edit.content, strip=True)
comment_form = CommentForm(instance=c_edit)
return render(request, 'submissions/edit_comment.html',
{'form': comment_form, 'sub_id': sub_id, 'comment':comment})
示例9: tamperWithForm
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
def tamperWithForm(self, **kwargs):
a = Article.objects.get(pk=1)
d = self.getValidData(a)
d.update(kwargs)
f = CommentForm(Article.objects.get(pk=1), data=d)
self.assertFalse(f.is_valid())
return f
示例10: publication_detail
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
def publication_detail(request, pk):
try :
object = Parution.objects.get(pk=pk)
comments = Comment.objects.filter(parution=object)
comment_form = CommentForm(request.POST or None)
context = {
"object":object,
"comments":comments,
}
if comment_form.is_valid():
comment_text = comment_form.cleaned_data['comment']
print(comment_text)
print(request.user)
print(object)
print(request.get_full_path())
print(object.get_absolute_url())
new_comment = Comment.objects.create_comment(user=request.user,text=comment_text,path=request.get_full_path(),parution=object)
return HttpResponseRedirect(object.get_absolute_url())
return render(request,"blogdef/parution_detail.html",context)
context["comment_form"]=comment_form
return render(request,"blogdef/parution_detail.html",context)
except :
raise Http404
示例11: add_comment
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
def add_comment(request, object_type, id):
skip = False
if object_type == 'comicpage':
object = get_object_or_404(ComicPage, pk=id)
elif object_type == 'article':
object = get_object_or_404(Article, pk=id)
elif object_type == 'profile':
object = get_object_or_404(Profile, pk=id)
friend_status = get_relationship_status(object.user, request.user)
if friend_status < 2 and object.user != request.user:
skip = True
elif object_type == 'tutorial':
object = get_object_or_404(Tutorial, pk=id)
elif object_type == 'episode':
object = get_object_or_404(Episode, pk=id)
elif object_type == 'video':
object = get_object_or_404(Video, pk=id)
if request.method == 'POST' and not skip:
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.author = request.user
comment.save()
object.comments.add(comment)
comments = object.comments.filter(is_deleted=False)
return render_to_response('comments/list.html', {'comments':comments}, context_instance=RequestContext(request))
示例12: postshow
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
def postshow(request, id):
if not request.user.is_authenticated():
raise Http404
instance = get_object_or_404(Post, id=id)
queryset = Post.objects.get(id=id)
initial_data = {
"content_type": instance.get_content_type,
"object_id": instance.id
}
form = CommentForm(request.POST or None, initial=initial_data)
if form.is_valid():
c_type = form.cleaned_data.get("content_type")
content_type = ContentType.objects.get(model=c_type)
obj_id = form.cleaned_data.get('object_id')
content_data = form.cleaned_data.get("content")
new_comment, created = Comment.objects.get_or_create(
user = request.user,
content_type= content_type,
object_id = obj_id,
content = content_data
)
if created:
print("Yeah it work")
# print(comment_from.cleaned_data)
comments = instance.comments
#comments = Comment.objects.filter(user=request.user)
#comments = Comment.objects.filter(post=instance)
context = {
'post': queryset,
'comments':comments,
'comment_from':form,
}
return render(request,"post/show.html",context)
示例13: sound
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
def sound(request, username, sound_id):
try:
sound = Sound.objects.select_related("license", "user", "user__profile", "pack", "remix_group").get(id=sound_id)
if sound.user.username.lower() != username.lower():
raise Http404
user_is_owner = request.user.is_authenticated() and (
sound.user == request.user
or request.user.is_superuser
or request.user.is_staff
or Group.objects.get(name="moderators") in request.user.groups.all()
)
# If the user is authenticated and this file is his, don't worry about moderation_state and processing_state
if user_is_owner:
if sound.moderation_state != "OK":
messages.add_message(request, messages.INFO, "Be advised, this file has <b>not been moderated</b> yet.")
if sound.processing_state != "OK":
messages.add_message(request, messages.INFO, "Be advised, this file has <b>not been processed</b> yet.")
else:
if sound.moderation_state != "OK" or sound.processing_state != "OK":
raise Http404
except Sound.DoesNotExist: # @UndefinedVariable
try:
DeletedSound.objects.get(sound_id=sound_id)
return render_to_response("sounds/deleted_sound.html", {}, context_instance=RequestContext(request))
except DeletedSound.DoesNotExist:
raise Http404
tags = sound.tags.select_related("tag__name")
if request.method == "POST":
form = CommentForm(request, request.POST)
if request.user.profile.is_blocked_for_spam_reports():
messages.add_message(
request,
messages.INFO,
"You're not allowed to post the comment because your account has been temporaly blocked after multiple spam reports",
)
else:
if form.is_valid():
comment_text = form.cleaned_data["comment"]
sound.comments.add(Comment(content_object=sound, user=request.user, comment=comment_text))
sound.num_comments = sound.num_comments + 1
sound.save()
try:
# send the user an email to notify him of the new comment!
logger.debug(
"Notifying user %s of a new comment by %s" % (sound.user.username, request.user.username)
)
send_mail_template(
u"You have a new comment.",
"sounds/email_new_comment.txt",
{"sound": sound, "user": request.user, "comment": comment_text},
None,
sound.user.email,
)
except Exception, e:
# if the email sending fails, ignore...
logger.error("Problem sending email to '%s' about new comment: %s" % (request.user.email, e))
return HttpResponseRedirect(sound.get_absolute_url())
示例14: blog
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
def blog(request, slug):
from comments.models import BLOG
blog = Blog.objects.get(slug=slug)
if not blog:
raise Http404
form = CommentForm()
if request.POST:
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.author = request.user
comment.thing_id = blog.id
comment.type = BLOG
comment.thread_id, comment.reverse_thread_id = \
get_thread_id(type='blog', thing_id=blog.id)
comment.save()
return HttpResponseRedirect(reverse(
'blog.views.blog',
args=(blog.slug,)))
commentsPage, commentsIndex, commentsTree = \
get_comments_page(request, type=BLOG, obj=blog, rpp=10)
return render_to_response(request, 'blog/blog.html', {
'blog': blog,
'commentForm': form,
'comments': commentsPage,
'commentsIndex': commentsIndex,
'commentsTree': commentsTree,
})
示例15: article_show
# 需要导入模块: from comments.forms import CommentForm [as 别名]
# 或者: from comments.forms.CommentForm import is_valid [as 别名]
def article_show(request, channel, slug):
try:
article = Article.published_detail.get(slug=slug, channel__slug=channel)
article.views_count_last_week += 1
article.views_count += 1
article.save()
except:
raise Http404(u'Notícia não encontrada')
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.submit_date = datetime.now()
comment.user = None
comment.site = Site.objects.get_current()
comment.ip_address = request.META.get("REMOTE_ADDR", None)
comment.content_type = ContentType.objects.get_for_model(article)
comment.object_pk = article.id
comment.save()
return HttpResponseRedirect(reverse('article_show',args=(article.channel.slug,article.slug)))
else:
form = CommentForm()
related_articles = article.get_related_articles()
return render_to_response('articles/article_show.html', {
'article': article,
'related_articles': related_articles,
'form':form,
'is_article':True,
}, context_instance=RequestContext(request))