1 回答

TA貢獻(xiàn)2011條經(jīng)驗 獲得超2個贊
您可以在您的上下文中傳遞一個值Today,例如基于類的視圖:
from django.views.generic import ListView
from django.utils import timezone
class MyView(ListView):
[...]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['Today'] = timezone.now().date()
return context
如果您需要有關(guān)向基于類的視圖添加額外上下文的詳細(xì)信息或上下文的簡短描述,請參見此處。
基于函數(shù)的視圖的示例:
from django.shortcuts import render
from django.utils import timezone
def my_view(request):
[...your code...]
context['Today'] = timezone.now().date()
return render(request, template_name="your_template.html",
context=context)
添加回答
舉報