您的位置:首页 > 编程语言 > Go语言

Django学习笔记六:form表单实现评论(检验数据合法性)

2017-08-02 16:09 751 查看
最近一直想给博客更新一个模板,找了好多的静态模板,不知道怎么用,这几天特意花点时间在这个事情上,主要是静态文件的存放路径,好复杂呀,花了近两天时间才豁然开朗,特找了一个模板放在博客上,同时完善了博客的评论功能。

静态模板来自点击打开链接(花了我5个大洋换了500个积分,然而就用了20个积分生气)

运行效果

先上图看看效果:(是不是比之前的好看多了)



替换模板是个繁琐的项目。在这里就不多记录啦。

主要记录学习到了评论功能的实现。

评论功能作为单独的一个功能,把它作为一个单独的app,关于django框架设计的基本流程都是一样的:

设计数据库(M)-设计模型(V)-编写模板(T)

省略前面的建立app

评论功能分析

属性(5)

姓名,邮箱,电话,评论内容,创建时间

关系

一篇文章可以有多个评论

一个评论只能是一篇文章

so:

文章-评论 一对多

知道了属性和关系,下面就很好设计了

数据库设计

# -*-coding:utf -*-
from django.db import models

# Create your models here.
class Comment(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField(max_length=255)
phone = models.CharField(max_length=20)
text = models.TextField()
created_time = models.DateTimeField(auto_now_add=True)

article = models.ForeignKey('blog.Article')

def __str__(self):
return self.text[:20]


设计评论表单模板

通常提交评论时需要检验评论者的邮箱,电话等内容是否正确,这里用到了django中form表单的功能,功能具体实现流程:



下面就是将流程转换为代码:

评论表单

comments/forms.py
# -** coding:utf-8 -*-
from django import forms
from .models import Comment

class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['name', 'email', 'phone','text']


绑定url和视图函数

comments/urls.py
from django.conf.urls import url
from . import views

app_name = 'comments'
urlpatterns = [

url(r'^comment/article/(?P<article_id>[0-9]+)/$', views.article_comment, name='article_comment')
]


视图函数

comments/views.py
# -*- coding:utf-8 -*-
from django.shortcuts import render, get_object_or_404, redirect
from blog.models import Article

from .models import Comment
from .forms import CommentForm
# Create your views here.
def article_comment(request, article_id):

#获取文章存在是获取给post, 反之返回404页面
article = get_object_or_404(Article, id = article_id)

#POST提交表单
if request.method == 'POST':
#生成表单
form = CommentForm(request.POST)

#django自动检验表单的内容是否合法
if form.is_valid():
#合法生成comment实例
comment = form.save(commit = False)
comment.article = article
comment.save()
return redirect(article)
else:
#不合法
comment_list= article.comment_set.all()
context = {'article':article,'form':form,'comment_list':comment_list}

return render(request, 'blog/detail.html', context=context)
#无提交命令,重定向到文章详情页
return redirect(article)


因为要将文章的评论加载在博客详情的页面中,这里的修改就是为了更新文章详情页,使其加载评论。

blog/view.py
def detail(request,id):
article = get_object_or_404(Article,id=id)
article.body = markdown.markdown(article.body,
extensions = [
'markdown.extensions.extra',
'markdown.extensions.codehilite',
'markdown.extensions.toc',
])
form = CommentForm()
comment_list = article.comment_set.all()
context = {
'article':article,
'form':form,
'comment_list':comment_list
}
return render(request, 'detail.html', context=context)


模型的设计

通过post方法提交表单。

templates/detail.html

<section class="comment-area" id="comment-area">
<hr>
<h3>发表评论</h3>
<form action="{% url 'comments:article_comment' article.id %}" method="post" class="comment-form">
{% csrf_token %}
<div class="row">
<div class="col-md-4">
<label for="{{ form.name.id_for_label }}">名字:</label>
{{ form.name}}
{{ form.name.errors }}
</div>
<div class="col-md-4">
<label for="{{ form.email.id_for_label }}">邮箱:</label>
{{ form.email }}
{{ form.email.errors }}
</div>
<div class="col-md-4">
<label for="{{ form.phone.id_for_label }}">联系方式(请备注qq or 微信):</label>
{{ form.phone }}
{{ form.phone.errors }}
</div>
<div class="col-md-4">
<label for="{{ form.text.id_for_label }}">评论:</label>
{{ form.text }}
{{ form.text.errors }}
<button type="submit" class="comment-btn">发表</button>
</div>
<div class="comment-list-panel">
<h3>评论列表:</h3>
<ul class="comment-list list-unstyled">
{% for comment in comment_list %}
<li class="comment-item">
<span class="nickname">{{ comment.name}}</span>
<time class="submit-date" >{{ comment.created_time }}</time>
<div class="text">
{{ comment.text }}
</div>
</li>
{% empty %}
暂无评论
{% endfor %}
</ul>
</div>
</div>    <!-- row -->

</form>

</section>


到这里就评论功能就实现啦。

下面贴两个测试运行的截图:



填写评论表单



显示

这里用到了重定向方法,填写评论之后不用刷新就可以看到更新的评论,也是我觉得最有意思的地方。

总结

django的表单的功能有很多,最神奇的就是可以自动检验数据的合法性,还有重定向函数。初次学习,总结的会有很多不对的地方,希望大家多多提出,共同学习。

本项目github链接:

https://github.com/xuna123/Django_study2

参考资料:

http://zmrenwu.com/post/14/

http://blog.csdn.net/marksinoberg/article/details/51622616
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: