Python 如何审查Django博客评论中的文字?
我已经在我的博客上应用了一个评论系统,我想用一个简单的代码来审查内容中的坏话。 我有一个badwords.txt,里面有所有的坏单词,可以作为字典来检查这个单词 我的问题是我不知道我应该在哪里应用这个代码 第二,我想知道由于使用了这些单词,如何将表单返回为无效 这是坏词代码Python 如何审查Django博客评论中的文字?,python,django,Python,Django,我已经在我的博客上应用了一个评论系统,我想用一个简单的代码来审查内容中的坏话。 我有一个badwords.txt,里面有所有的坏单词,可以作为字典来检查这个单词 我的问题是我不知道我应该在哪里应用这个代码 第二,我想知道由于使用了这些单词,如何将表单返回为无效 这是坏词代码 import fileinput filename = input("Enter a file name: ") censor = input("Enter the curse word th
import fileinput
filename = input("Enter a file name: ")
censor = input("Enter the curse word that you want censored: ")
for line in fileinput.input(filename, inplace=True):
line = line.replace(censor, 'CENSORED')
print(line, end='')
这是我正在从事的Django项目
这是模型
class Comment(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
content = models.TextField(max_length=300)
这是views.py
class PostDetailView(DetailView):
model = Post
template_name = "blog/post_detail.html" # <app>/<model>_<viewtype>.html
def get_context_data(self, *args, **kwargs):
context = super(PostDetailView, self).get_context_data()
post = get_object_or_404(Post, slug=self.kwargs['slug'])
comments = Comment.objects.filter(
post=post).order_by('-id')
if self.request.method == 'POST':
comment_form = CommentForm(self.request.POST or None)
if comment_form.is_valid():
content = self.request.POST.get('content')
comment_qs = None
comment = Comment.objects.create(
post=post, user=self.request.user, content=content)
comment.save()
return HttpResponseRedirect("blog/post_detail.html")
else:
comment_form = CommentForm()
context["comments"] = comments
context["comment_form"] = comment_form
return context
def get(self, request, *args, **kwargs):
res = super().get(request, *args, **kwargs)
self.object.incrementViewCount()
return res
class PostCommentCreateView(LoginRequiredMixin, CreateView):
model = Comment
form_class = CommentForm
def form_valid(self, form):
post = get_object_or_404(Post, slug=self.kwargs['slug'])
form.instance.user = self.request.user
form.instance.post = post
return super().form_valid(form)
def form_invalid(self, form):
return HttpResponseRedirect(self.get_success_url())
def get_success_url(self):
return reverse('blog:post-detail', kwargs=dict(slug=self.kwargs['slug']))
class PostDetailView(详细视图):
型号=员额
template_name=“blog/post_detail.html”#/35;.html
def获取上下文数据(self、*args、**kwargs):
context=super(PostDetailView,self).get_context_data()
post=获取对象或404(post,slug=self.kwargs['slug'])
comments=Comment.objects.filter(
post=post).订单作者('-id')
如果self.request.method==“POST”:
comment\u form=CommentForm(self.request.POST或None)
如果注释形式有效():
content=self.request.POST.get('content')
注释=无
comment=comment.objects.create(
post=post,user=self.request.user,content=content)
comment.save()
返回HttpResponseRedirect(“blog/post_detail.html”)
其他:
注释形式=注释形式()
上下文[“注释”]=注释
上下文[“注释形式”]=注释形式
返回上下文
def get(自我、请求、*args、**kwargs):
res=super().get(请求,*args,**kwargs)
self.object.incrementViewCount()
返回res
类PostCommentCreateView(LoginRequiredMixin,CreateView):
模型=注释
form_class=CommentForm
def表单_有效(自身、表单):
post=获取对象或404(post,slug=self.kwargs['slug'])
form.instance.user=self.request.user
form.instance.post=post
返回super().form_有效(form)
def表单_无效(自身、表单):
返回HttpResponseRedirect(self.get_success_url())
def get_success_url(自我):
返回反向('blog:post-detail',kwargs=dict(slug=self.kwargs['slug']))
Django有一个名为的功能,您可以使用它来确保评论不包含任何经过审查的单词
您可能想在某个地方找到一些经过审查的单词列表,您可以从文件中读取这些单词:
以open(“badwords.txt”)作为f:
删失单词=f.读线()
您可以具有如下验证程序功能:
import re
def validate_comment_text(text):
words = set(re.sub("[^\w]", " ", text).split())
if any(censored_word in words for censored_word in CENSORED_WORDS):
raise ValidationError(f"{censored_word} is censored!")
在注释
类中,您可以使用此验证器:
content=models.TextField(最大长度=300,验证器=[validate\u comment\u text])
Django有一个名为的功能,您可以使用它来确保评论不包含任何经过审查的单词
您可能想在某个地方找到一些经过审查的单词列表,您可以从文件中读取这些单词:
以open(“badwords.txt”)作为f:
删失单词=f.读线()
您可以具有如下验证程序功能:
import re
def validate_comment_text(text):
words = set(re.sub("[^\w]", " ", text).split())
if any(censored_word in words for censored_word in CENSORED_WORDS):
raise ValidationError(f"{censored_word} is censored!")
在注释
类中,您可以使用此验证器:
content=models.TextField(最大长度=300,验证器=[validate\u comment\u text])
views.py或models.py中的validate_comment_文本应该放在哪里。pyI会将其放在models.pyI中。pyI在注释(models.Model)之前添加了验证器和所有内容,但没有起作用。它没有显示任何错误。只是为了确认,您还更新了注释类以使用验证程序?是的,我添加了content=models.TextField(最大长度=300,验证程序=[validate\u Comment\u text])
views.py或models.py中的validate_comment_文本应该放在哪里。pyI会将其放在models.pyI中。pyI在注释(models.Model)之前添加了验证器和所有内容,但没有起作用。它没有显示任何错误。只是为了确认,您还更新了注释
类以使用验证程序?是的,我添加了content=models.TextField(最大长度=300,验证程序=[validate\u Comment\u text])