django前后端不分离入门

在django中创建html模板,关联数据,返回给浏览器

修改settings.py文件

将 'DIRS': [] 修改为'DIRS': [os.path.join(BASE_DIR, 'templates')],

项目目录下创建模板文件夹templates

templates下创建模板文件index.html





Title


{{key0}}  {{value0}}

{{key10}}  {{value10}}



views.py编写

from django.shortcuts import render
from django.http import HttpResponse
from django.core import serializers

def get_data(request):
...
return render(request, "index.html",{"key0": "11", "value0": "22"})
#return render(request, "err.html", {"err": "404"})

urls.py指定路由

...

浏览器访问测试

html文件有修改,保存并刷新浏览器即可显示最新内容。

其他

https://www.cnblogs.com/my_life/articles/8385635.html

posted @ 2022-09-05 22:22  tangshow  阅读(195)  评论(0)    收藏  举报