Django博客功能实现—文章评论功能

功能:在A网页提交一个评论Forms_B,提交之后自动刷新页面,能够显示刚刚的画面
思路:利用一个已经创建的表单,通过视图让其在网页中表现出来,填写玩信息之后提交,会提交到一个新的视图里面去做接受,接受之后重定向到另外一个地方。(是什么地方)
步骤:

一、在app里面的forms.py里面新建一个表单:

 #blog/forms.py
from django import forms class CommentForm(forms.Form):
'''
评论表单
'''
#author使用CharField字段,字段内使用三个参数,分别是widget, max_length, error_messages
author = forms.CharField(
#为各个需要渲染的字段指定渲染成什么Html组件,主要是为了添加css样式
widget=forms.TextInput(attrs={"id": "author", "class": "comment_input",
"required": "required","size": "", "tabindex": ""}),
max_length=50,error_messages={"required":"username不能为空",})
email = forms.EmailField(widget=forms.TextInput(attrs={"id":"email","type":"email","class": "comment_input",
"required":"required","size":"", "tabindex":""}),
max_length=50, error_messages={"required":"email不能为空",})
url = forms.URLField(widget=forms.TextInput(attrs={"id":"url","type":"url","class": "comment_input",
"size":"", "tabindex":""}),
max_length=100, required=False)
comment = forms.CharField(widget=forms.Textarea(attrs={"id":"comment","class": "message_input",
"required": "required", "cols": "",
"rows": "", "tabindex": ""}),
error_messages={"required":"评论不能为空",})
article = forms.CharField(widget=forms.HiddenInput())

二、在文章详情视图里面让接入表单

 def article(request):
# 评论表单
comment_form = CommentForm({'author': request.user.username,
'email': request.user.email,
'url': request.user.url,
'article': id} if request.user.is_authenticated() else{'article': id})
return render(request, 'article.html', locals())

三、在模板中将视图体现出来

     <form action="{% url 'comment_post' %}" method="post">
{% csrf_token %}
<p>{{ comment_form.author }}
<label for="author">Name (required)</label></p> <p>{{ comment_form.email }}
<label for="email">Email (Will NOT be published) (required)</label></p> <p>{{ comment_form.url }}
<label for="url">URL</label></p> <p>{{ comment_form.comment }}</p> <p>
{{ comment_form.article }}
<input name="submit" type="submit" id="submit" tabindex="5" value="Submit" class="button" />
</p>
</form>

四、模板中的提交链接到URL中

url(r'^comment/post/$', comment_post, name='comment_post'),

五、在视图中接收提交的内容,并且重定向

 # 提交评论
def comment_post(request):
try:
#获取表单内填入的内容
comment_form=CommentForm(request.POST)
#进行验证的第一个表单验证
if comment_form.is_vaild():
#获取表单信息
#cleaned_data()用来把接收到的文字清洗为符合django的字符
#create是一个在一步操作中同时创建对象并且保存的便捷方法。
comment=Comment.objects.create(username=comment_form.cleaned_data["author"],
eamil=comment_form.cleaned_data["email"],
url=comment_form.cleaned_data["url"],
content=comment_form.cleaned_data["comment"],
article_id=comment_form.cleaned_data["article"],
#如果用户已经登录,则获取已经登录的用户,非登录用户将返回None
#此处用的if语句有些特殊。
user=request.user if request.user.is_authenticated() else None)
comment.save() #保存实例
else:
return render(request, "failure.html", {"reason":comment_form.erros})
except Exception as e:
logger.error(e)
#重定向到进来之前的网页
#HTTP_REFERER是http的头文件的一部分,当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器借此获得一些信息用于处理。
return redirect(request.META['HTTP_REFERER'])
'''
#判断用户是不是登录,也可以用这个写法,79行的写法也很简单
if request.user.is_authenticated(): #判断用户是否登录
user=request.user #获取已经登录的用户
else:
user=request.user #非登录用户将返回AnonymousUser
'''

request.META是一个里面包含了所有本次http请求的Header信息,比如用户IP,用户Agent
常见的键值有:
HTTP_REFERER,进站前链接网页,如果有的话。
redirec是重定向
redirec是重定向

上一篇:Java 核心卷学习笔记(一)


下一篇:Oracle 把秒转成时分秒格式(hh24:mm:ss);检测字符串是否是数字;字符串转换为数字