本文整理汇总了Python中blog.forms.CommentForm.is_valid方法的典型用法代码示例。如果您正苦于以下问题:Python CommentForm.is_valid方法的具体用法?Python CommentForm.is_valid怎么用?Python CommentForm.is_valid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类blog.forms.CommentForm
的用法示例。
在下文中一共展示了CommentForm.is_valid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post_comment_ajax
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def post_comment_ajax(request, object_id):
try:
entry = Entry.published_objects.get(pk=object_id)
except:
raise Http404
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
r = re.compile('(\w+\s+\n)|[<>"=]', re.I)
#r = re.compile('[\w\s\n]?', re.I)
#r = re.compile('<a href=', re.I)
if not r.search(form.cleaned_data['body']) == None:
raise Http404
add_comment(form, entry)
page = HttpResponseRedirect('/blog/comment/' + object_id + '/')
page.set_cookie('commentName', form.cleaned_data['author'].encode('utf-8'), expires=cookie_expires_value(), path='/', domain=None, secure=None)
return no_cache_page(page)
else:
form = CommentForm()
page = render_to_response('entry_comment.html', {'comments': entry.published_comment_list(), 'entry': entry, 'form': form})
return no_cache_page(page)
示例2: comment
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def comment(request, id):
"""
Add a comment to blog
"""
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
data = form.cleaned_data
comment = Comment()
comment.comment = data['comment']
comment.blog_id = id
comment.user_name = data['user_name']
comment.email = data['email']
comment.source_address = request.META['REMOTE_ADDR']# '192.168.2.8'
comment.create_date = datetime.datetime.now()
comment.save()
blog = Blog.objects.get(id=id)
if blog.comment_count is None:
blog.comment_count = 0
blog.comment_count += 1
blog.save()
if request.user.is_authenticated():
return HttpResponseRedirect('/adminshow/%s/' % id)
else:
return HttpResponseRedirect('/show/%s/' % id)
else:
blog = Blog.objects.get(id=id)
comments = Comment.objects.filter(blog_id=id)
return render_to_response('artical.html', {'blog' : blog, 'comments' : comments, 'form' : form}, context_instance=RequestContext(request, processors=[new_blog, blog_group]))
else:
return HttpResponseRedirect('/show/%s/' % id)
示例3: index
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def index(request):
entries = Blog.objects.all()
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
comments = form.cleaned_data['comments']
isSpam = consume(comments)
closeWriters(writer_1, writer_2)
updateSpamScore(isSpam)
updateSvm(collectionSpamScores, scores[0])
writeSpamScores(spamScoresFilename)
if isSpam:
return render_to_response('postFail.html', locals())
else:
return render_to_response('postSuccess.html', locals())
else:
form = CommentForm()
return render_to_response('index.html', locals())
示例4: comment
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def comment(req):
if req.method == 'POST':
form = CommentForm(req.POST) # 换成表单
if form.is_valid():
comment = form.save()
return HttpResponseRedirect('/comment/')
comment_list = Comment.objects.all()
paginator = Paginator(comment_list, 10)
num_pages = paginator.num_pages
pages = range(1, num_pages + 1)
width = num_pages * 34
# Make sure page request is an int. If not, deliver first page.
try:
page = int(req.GET.get('page', '1'))
except ValueError:
page = 1
# If page request (9999) is out of range, deliver last page of results.
try:
comments = paginator.page(page)
except (EmptyPage, InvalidPage):
comments = paginator.page(paginator.num_pages)
return render(req, 'comment.html', {
'comments': comments,
'tests': comment_list,
'pages': pages,
'current_page': int(page),
'width': width,
})
示例5: comment
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def comment(request, post_id):
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
if not request.user.is_authenticated():
r = captcha.submit(request.POST['recaptcha_challenge_field'], request.POST['recaptcha_response_field'], settings.RECAPTCHA_PRIVATE_KEY, request.META['REMOTE_ADDR'])
if not r.is_valid:
return redirect('/blog/'+post_id+'/') # Invalid form entry
f = form.save(commit=False);
f.post_id = post_id
if request.user.is_authenticated():
f.author_id = request.user.id
f.author_name = ''
f.author_email = ''
f.save()
email_msg = 'You have a new comment at http://www.jessicasteiber.com/blog/'+post_id+'/#comments'
email_from = '[email protected]'
email_to = ['[email protected]', '[email protected]']
# Send email notification
send_mail('New Blog Comment - JessicaSteiber.com', email_msg, email_from, email_to)
return redirect('/blog/'+post_id+'/#comments') # Go back to blog with new comment
else:
return redirect('/blog/'+post_id+'/') # Invalid form entry
else:
return redirect('/blog/'+post_id+'/') # Go back to blog
示例6: comment
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def comment(request, post_id):
if request.method == 'POST':
post_obj = Post.objects.get(id = post_id)
mail = request.POST.get('mail')
username = request.POST.get('username')
message = request.POST.get('message')
post = request.POST.get('post', post_id)
data = {
'mail' : mail,
'username' : username,
'message' : message,
'post' : post_id
}
form = CommentForm(data)
if form.is_valid():
comment = Comment(mail= mail, username=username, message=message, post=post_obj)
comment.save()
request.session['comment_error'] = False
request.session['comment_success'] = True
else:
request.session['comment_success'] = False
request.session['comment_error'] = True
else:
form = CommentForm()
return HttpResponseRedirect(reverse('post_details', kwargs={'slug': post_obj.slug}))
示例7: post_detail
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post, status='published',
publish__year=year, publish__month=month,
publish__day=day)
comments = post.comments.filter(active=True)
if request.method == 'POST':
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
new_comment = comment_form.save(commit=False)
new_comment.post = post
new_comment.save()
# XXX: fix this, can't find post after submitting
return HttpResponseRedirect(reverse('blog:post_detail', kwargs={'year': year,
'month': month,
'day': day,
'post': post}))
else:
comment_form = CommentForm()
post_tags_ids = post.tags.values_list('id', flat=True)
similar_posts = Post.published.filter(tags__in=post_tags_ids).exclude(id=post.id)
similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by('-same_tags',
'-publish')[:4]
return render(request, 'blog/post/detail.html', {'post': post, 'comments': comments,
'comment_form': comment_form,
'similar_posts': similar_posts})
示例8: post
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def post(request, username, post_id):
try:
template = 'blog/post.html'
args = {}
required_post = Post.objects.get(id=post_id)
if not required_post.visible and required_post.user != request.user:
raise Http404()
args.update({'post': required_post})
args.update({'comments': Comment.objects.filter(post=post_id)})
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
Comment(post=Post.objects.get(id=post_id),
user=User.objects.get(username=request.user.username),
body=form.cleaned_data['body']).save()
return HttpResponseRedirect('/' + username + '/post' + post_id)
else:
form = CommentForm()
args.update({'form': form})
except:
raise Http404()
return render_to_response(template, args, context_instance=RequestContext(request))
示例9: view_entry
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def view_entry(request, e_id):
""" View Entry """
entry = Entry.objects.get(id=e_id)
entry.parsed_content = mark_safe(entry.content) #bbcode.render_html(entry.content)
msg = ''
if(request.method == 'POST'):
user_data= {'author': request.POST['author'] , 'email': request.POST['email'], 'message': request.POST['message'],'captcha': request.POST['captcha'] }
#check captcha
code = request.session['captcha_code']
if(request.user.is_authenticated()):
#user auth witrh external account
user_data['author'] = '%s %s '%(request.user.first_name, request.user.last_name )
if(request.session['backend'] == 'twitter'):
user_data['author'] += '(@%s)'%(request.user.username)
else:
user_data['author'] +='(%s)'%(request.session['backend'])
if(request.user.email == ''):
user_data['email'] = '[email protected]'#request.user.email
else:
user_data['email'] = request.user.email
logout(request)
cf = CommentForm(user_data)
if(cf.is_valid()):
#Check Captcha
if(''.join(code) == str.strip(str(request.POST['captcha'].upper()))):
#save comment
com = Comment()
com.author = cf.cleaned_data['author']
com.content = cf.cleaned_data['message']
com.email = cf.cleaned_data['email']
com.entry = entry
try:
com.save()
msg = 'Comment posted succesfully. Thanks.!'
except:
msg = 'Error saving your comment. Please try again.'
else:
msg = 'Wrong Captcha code. Please Try Again'
else:
msg = 'Error processing your comment. Please Try Again.'
request.session['comment_posted_msg'] = msg
return redirect('/blog/article/%s'%(e_id))#TODO put marker here to go to specific part of the html
if('comment_posted_msg' in request.session):
msg= request.session['comment_posted_msg']
del request.session['comment_posted_msg']
comments = entry.comment_set.filter(status=True)
cf = CommentForm()
t = get_template("entry.htm")
c = RequestContext(request, {'entry': entry,'comments': comments, 'cform': cf, 'rn': random.randint(1,999999), 'msg': msg})
return HttpResponse(t.render(c))
示例10: lire_article
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def lire_article(request, slug):
"""
Affiche un article complet, sélectionné en fonction du slug
fourni en paramètre
"""
article = get_object_or_404(Article, slug=slug)
comments = Comment.objects.filter(article=article)
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
# ajouter la relation avec l'article
comment = Comment()
comment.pseudo = form.cleaned_data['pseudo']
comment.email = form.cleaned_data['email']
comment.contenu = form.cleaned_data['contenu']
comment.article = article
comment.save()
renvoi = True
else:
form = CommentForm()
return render(request, 'blog/lire_article.html', locals())
示例11: entry_detail
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def entry_detail(request, year, month, day, slug, draft=False):
date = datetime.date(*time.strptime(year+month+day, '%Y'+'%b'+'%d')[:3])
entry = get_object_or_404(Entry, slug=slug,
created_on__range=(
datetime.datetime.combine(date, datetime.time.min),
datetime.datetime.combine(date, datetime.time.max)
), is_draft=draft)
if request.method == 'POST' and entry.comments_allowed():
form = CommentForm(request.POST)
if form.is_valid():
comment = Comment(**form.cleaned_data)
comment.entry = entry
if request.META['REMOTE_ADDR'] != '':
comment.ip = request.META['REMOTE_ADDR']
else:
comment.ip = request.META['REMOTE_HOST']
comment.date = datetime.datetime.now()
comment.karma = 0
comment.spam = akismet(request, comment)
comment.save()
if (not comment.spam) and settings.BLOG_COMMENT_EMAIL:
comment_email = "%s\n--\n%s\n%s\n%s" % (comment.comment,
comment.name, comment.email, comment.website)
send_mail('[Blog] %s' % entry.title, comment_email,
comment.email, [entry.author.email], fail_silently=True)
return HttpResponseRedirect(entry.get_absolute_url())
else:
form = CommentForm()
return render_to_response('blog/entry_detail.html',
{'blog_title': settings.BLOG_TITLE, 'tags': Tag.objects.all(),
'object': entry, 'comment_form': form},
context_instance=RequestContext(request))
示例12: detail
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def detail(request, p_id):
context = RequestContext(request)
tags = Tag.objects.all()
context_dict = {'tags': tags}
post = get_object_or_404(Post, id=p_id)
context_dict['post'] = post
comments = Comment.objects.filter(post=post)
context_dict['comments'] = comments
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.post = post
comment.save()
return redirect('detail', p_id=p_id)
else:
form = CommentForm()
context_dict['comment_form'] = form
return render_to_response('blog/detail.html',
context_dict, context)
示例13: post_detail
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def post_detail(request, slug):
post = get_object_or_404(Post.pub, slug=slug)
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.post = post
comment.ip_address = request.META.get("REMOTE_ADDR", None)
comment = form.moderate(comment)
comment.save()
email_body = "%s posted a new comment on the post '%s'."
mail_managers("New comment posted", email_body %
(comment.user_name, post))
return redirect('blog.views.post_detail', slug=post.slug)
else:
form = CommentForm()
return render_to_response('blog/post_detail.html', {
'post': post,
'form': form,
})
示例14: get_post
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def get_post(request, slug):
post = get_object_or_404(Post, slug=slug)
comments = Comment.objects.filter(post=post)
if request.method == "POST":
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
parent_id = request.POST.get('parent')
text = request.POST.get('text')
if parent_id:
parent = get_object_or_404(Comment, id=int(parent_id))
comment = Comment(post=post, text=text, author=request.user, parent=parent)
else:
comment = Comment(post=post, text=text, author=request.user,)
comment.save()
return http.HttpResponseRedirect(request.path)
else:
comment_form = CommentForm()
response = render(request, 'post.html', {
'post': post,
'comments': comments,
'comment_form': comment_form
})
cookie_name = 'viewed_%s' % post.id
if cookie_name not in request.COOKIES:
response.set_cookie(cookie_name, '1', 18000)
Post.objects.filter(slug=slug).update(views=F('views') + 1)
return response
示例15: get_article
# 需要导入模块: from blog.forms import CommentForm [as 别名]
# 或者: from blog.forms.CommentForm import is_valid [as 别名]
def get_article(request,slug):
try:
article=Article.objects.get(slug=slug)
except ObjectDoesNotExist:
return HttpResponseRedirect('/')
try:
comments=Comment.objects.filter(article=article).order_by('-date','-id')
except ObjectDoesNotExist:
comments=None
if request.method == 'POST':
form = CommentForm(request.POST, **{'article':article,'ip':request.META['REMOTE_ADDR'],})
if form.is_valid():
comment=Comment()
comment.comment = form.cleaned_data['comment']
comment.article = article
if request.user.is_authenticated():
comment.user = request.user
comment.ip=request.META['REMOTE_ADDR']
comment.save()
return HttpResponseRedirect('/'+article.slug)
else:
form = CommentForm()
return render_to_response('blog/article.html', {
'form': form,
'article': article,
'comments':comments,
},context_instance=RequestContext(request))