所以,我的问题是我有一个detailview,它显示了我数据库中的特定帖子。然后,我使用get_context_data然后从不同的模型中获取db值;但是,它在我的模板中输出了一些奇怪的东西。
我可以在模板中更改哪些内容,以便从其他模型中列出每个正确的数据库值?
models.py
class Projects(models.Model):
user = models.ForeignKey(User)
slug = models.SlugField()
project_title = models.CharField(max_length=30)
project_shortdesc = models.CharField(max_length=248)
project_desc = models.TextField()
def save(self):
super(Projects, self).save()
date = datetime.date.today()
self.slug = '%i%i%i%s' % (
date.year, date.month, date.day, slugify(self.project_title)
)
super(Projects, self).save()
class ProjectsToDo(models.Model):
project_tododate = models.DateField()
project_tododesc = models.TextField(max_length = 500)
project_id = models.ManyToManyField(Projects)
views.py
class ProjectDetail(generic.DetailView):
model = Projects
context_object_name = 'indprojects'
template_name = 'projectpage.html'
def get_context_data(self, *args, **kwargs):
context = super(ProjectDetail, self).get_context_data(*args, **kwargs)
context['todolist'] = ProjectsToDo.objects.order_by('project_tododate')
context['todoform'] = ProjectToDoForm()
context['form'] = ProjectForm(instance=Projects.objects.get(slug=self.kwargs['slug']))
return context
def get_queryset(self):
return Projects.objects.filter(user=self.request.user)
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(ProjectDetail, self).dispatch(request, *args, **kwargs)
模板
{{todolist}}
这来自模板,输出:
[<ProjectsToDo: ProjectsToDo object>, <ProjectsToDo: ProjectsToDo object>]
我已经尝试了{{todolist.project_tododesc}}
,并且都没有输出任何数据。我不确定如何解决这个问题,任何帮助都会受到赞赏。
答案 0 :(得分:0)
我认为问题在于,如果在类__str__()
中使用Python 3(或Python 2.x中的__unicode__()
),则没有定义ProjectsToDo
方法。
当您尝试打印模型实例时,会发生什么,Django会查看__str__()
来决定要显示的内容。