[英]How to pass a value to the template in class based views in django?
我想將變量的值發送到PostDetail
模板,這是views.py
文件的功能
class PostDetailView(DetailView):
model = Post
def get_object(self):
obj = super().get_object()
obj.view_count += 1
obj.save()
return obj
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(**kwargs)
texts = self.object.content
Read_Time=get_read_time(texts)
print(Read_Time)
return context
這是終端的輸出:-
[04/Sep/2020 19:29:04] "GET /post/this-blog-contains-an-image-with-it/ HTTP/1.1" 200 6089
0:02:00
[04/Sep/2020 19:29:24] "GET /post/this-blog-contains-an-image-with-it/ HTTP/1.1" 200 6089
我想將0:02:00
發送到我的模板,我怎樣才能做到這一點?
我更願意在Post
模型中創建一個方法
class Post(models.Model):
# your fields
def read_time(self): return get_read_time(self.content)
然后您可以通過以下方式訪問模板中的讀取時間
{{ object.read_time }}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.