我想生成我的模板表,顯示了一些數據,我從一個方法獲取宣佈對我的任務類如何調用模型方法,在Django中使用對象作爲參數?
models.py
class Task(models.Model):
...
def check_if_finished(self):
resp = requests.get(
'http://my.rest.api/tasks/view/{}'.format(self.task_id))
resp_data = resp.json()
resp_finished = resp_data['task']['started_on']
if resp_finished is None:
return False
else:
return resp_finished
我知道這是沒有意義的調用方法在模板上,但我需要用什麼來顯示這些數據?
template.html
{{task.is_finished(task.task_id)}}
當我嘗試我得到此錯誤消息: >無法解析餘額:'(task.task_id)'從'task.check_if_finished(task.task_id)' –
哦,這是一個模板錯誤。我只是看着它,似乎你不能用Django模板中的參數調用函數/方法。您需要創建自定義模板標記(https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags)或在視圖中進行檢查併發送將正確的值放入模板中。 –
對於我的'Task.objects.all()'我想通過API請求來檢查任務是否完成。然後,我想在表格中顯示該任務是否完成。 –