Python+Django 空格分隔数据并传到前端页面

# urls.py
url('show/', views.show),

加入数据库里表的名称为“test”,其中有体格字段名称为“keyword”,其中存储着类似“香蕉 橘子 苹果”的数据,即每个词之间用空格“ ”隔开,我们在前端需要按空格分开显示数据。

# views.py
import re

def show(request,param):
    post = models.Test.objects.all()
    kw = post.keyword
    res = re.split(" ", kw)
    return render(request,"show.html",{"res": res})

在前端显示

<!-- show.html -->
关键字: {% for t in res %}
<a>{{ t }}</a> {% endfor %}

猜你喜欢

转载自www.cnblogs.com/hjy415/p/10941834.html