一, 在views.py文件中新增一个对象,用于向html提交数据
方式一: HttpResponse,无法提交后端数据库数据
def index(request):
return HttpResponse("welcome to django!")
方式二:render,可提交后端数据库数据
def html_index(request):
title="个人博客"
categories=models.Category.objects.all()
data={"title":title,"categories":categories,"articles":articles}
return render(request,'index.html',data)
def html_index(request):
title="个人博客"
categories=models.Category.objects.all()
return render(request,'index.html',locals())
render()函数,通过context传递数据给模版:
help文档中render描述:
render(request, template_name, context=None, content_type=None, status=None, using=None)
参数:
request: 是一个固定参数
template_name: templates中定义的文件,注意路径名。比如:"templates/polls/index.html", 则参数