在應用 json 方法之前,我{{filt.body|striptags|safe|slice:":250" }}在我的 html 模板中使用它來轉義所有特殊字符。現在我從 json 加載資料以及如何在我的 html 模板中應用上述方法。
模型.py
class Blog(models.Model):
body = RichTextUploadingField()
視圖.py
class PostJsonListView(View):
def get(self, *args, **kwargs):
posts = list(Blog.objects.all().filter(is_published='published') )
data = list()
for i in posts:
data.append({'body':i.body})
return JsonResponse({'data':data},safe=False)
uj5u.com熱心網友回復:
您可以像這樣在您的視圖中執行此操作:
from django.utils.safestring import SafeString
from django.utils.html import strip_tags
for i in posts:
body = strip_tags(SafeString(i.body))[:250]
data.append({'body': body})
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/420497.html
標籤:
下一篇:提取通過字串傳入的某些值
