初学者问题:过滤日期以匹配 Django 中另一个模型的日期
Beginner question: Filtering dates to match the date of another model in Django
这里是 Django 的初学者,过去几天我一直在断断续续地尝试解决这个问题,并尝试阅读文档和谷歌搜索解决方案,但无济于事。
我的 django 项目有两个应用程序 - 博客和天气,每个都有一个模型。
blog.models
import datetime
from django.db import models
class BlogEntry(models.Model):
date = models.DateField('Date')
blog_name = models.CharField('Name',max_length = 255, null=True)
weather.models
import datetime
from django.db import models
class WeatherEntry(models.Model):
date = models.DateField('Date')
weather = models.CharField('Weather',max_length = 255, null=True)
天气应用是每天填写一次,但博客应用只是偶尔填写。现在,我想为我的 BlogEntry 模型生成一个 DetailView,它还会显示创建博客当天的天气。我的计划是覆盖 get_context_data()
但我无法根据日期过滤天气查询集。
在视图中
import datetime
from django.views.generic.detail import DetailView
from blog.models import BlogEntry
from weather.models import WeatherEntry
class BlogDetailView(DetailView):
model = BlogEntry
template_name = 'Blog/blog_detail.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['weather'] = WeatherEntry.objects.filter(date__=date)
return context
我会得到错误 NameError: name 'date' is not defined
。我也用过 F 表达式,但它们也不起作用:(
错误很明显。您没有定义日期变量。那是因为没有日期变量定义或传递给 get_context_data
方法。首先,您必须确保日期变量已定义或已传递给 get_context_data
方法。其次,修正你的缩进方法。 python 中的缩进很重要。应该是这样
class BlogDetailView(DetailView):
model = BlogEntry
template_name = 'Blog/blog_detail.html'
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(**kwargs)
context['weather'] = WeatherEntry.objects.filter(date=self.object.date)
return context
这里是 Django 的初学者,过去几天我一直在断断续续地尝试解决这个问题,并尝试阅读文档和谷歌搜索解决方案,但无济于事。
我的 django 项目有两个应用程序 - 博客和天气,每个都有一个模型。
blog.models
import datetime
from django.db import models
class BlogEntry(models.Model):
date = models.DateField('Date')
blog_name = models.CharField('Name',max_length = 255, null=True)
weather.models
import datetime
from django.db import models
class WeatherEntry(models.Model):
date = models.DateField('Date')
weather = models.CharField('Weather',max_length = 255, null=True)
天气应用是每天填写一次,但博客应用只是偶尔填写。现在,我想为我的 BlogEntry 模型生成一个 DetailView,它还会显示创建博客当天的天气。我的计划是覆盖 get_context_data()
但我无法根据日期过滤天气查询集。
在视图中
import datetime
from django.views.generic.detail import DetailView
from blog.models import BlogEntry
from weather.models import WeatherEntry
class BlogDetailView(DetailView):
model = BlogEntry
template_name = 'Blog/blog_detail.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['weather'] = WeatherEntry.objects.filter(date__=date)
return context
我会得到错误 NameError: name 'date' is not defined
。我也用过 F 表达式,但它们也不起作用:(
错误很明显。您没有定义日期变量。那是因为没有日期变量定义或传递给 get_context_data
方法。首先,您必须确保日期变量已定义或已传递给 get_context_data
方法。其次,修正你的缩进方法。 python 中的缩进很重要。应该是这样
class BlogDetailView(DetailView):
model = BlogEntry
template_name = 'Blog/blog_detail.html'
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(**kwargs)
context['weather'] = WeatherEntry.objects.filter(date=self.object.date)
return context