Django_Learning Part3-表单POST

1.渲染表单(将表单渲染成input结构)

models.py添加

class Comment(models.Model):
    name = models.CharField(null=True, blank=True, max_length=50)
    comment = models.TextField(null=True, blank=True)
    def __str__(self):
        return self.comment

views.py添加

def detail(request):
    context = {}
    comment_list = Comment.objects.all()
    context['comment_list'] = comment_list
    return render(request,'detail_name.html',context)

urls.py添加

from firstapp.views import index, detail
urlpatterns = [
    url(r'^detail', detail, name='detail'),

2.绑定表单(用post方法向服务器提交评论,views校验数据)

def detail(request):
    if request.method == 'GET':
        form = CommentForm
    if request.method == 'POST'
        from = CommentForm(request.POST)

3.返回交验结果(检结果装入变量,返回到模板中渲染)

猜你喜欢

转载自blog.csdn.net/weixin_40047053/article/details/80513796