Django DetailView get_context_data -
i'm new in django. there html page (project_details) should show title , tasks of project, shows title of project, not tasks. tasks exists, problem filter!!!
views.py the error here
from .models import project,task django.views.generic import listview, detailview class projectslist(listview): template_name = 'projects_list.html' queryset= project.objects.all() class projectdetail(detailview): model = project template_name = 'projects_details.html' def get_context_data(self, **kwargs): context = super(projectdetail, self).get_context_data(**kwargs) ## context list of tasks of project## ##this error## context['tasks'] = task.object.filter(list=project) <---->here ((work task.object.all() )) return context
models.py
class project(models.model): title = models.charfield(max_length=30) slug = autoslugfield(populate_from='title', editable=false, always_update=true) class task(models.model): title = models.charfield(max_length=250) list = models.foreignkey(project) slug = autoslugfield(populate_from='title', editable=false, always_update=true)
urls.py
from django.conf.urls import url .models import project .views import projectslist, projectdetail urlpatterns = [ url(r'^$', projectslist.as_view(), name='project_list'), url(r'(?p<slug>[\w-]+)/$',projectdetail.as_view() , name='project_details'),]
projects_details.html
{% extends './base.html' %} {% block content %} <div> <a href={{ object.get_absolute_url }}> <h4> {{object.title}} </h4> </a> <ul> {% task in tasks %} <----> no output <li> <li> {{task}}</li> {% endfor %} </ul> </div> {% endblock content %}
sorry bad english.
project
model class, doing (list=project)
doesn't make sense.
if want access object in detail view's get_context_data
method, can use self.object
:
def get_context_data(self, **kwargs): context = super(projectdetail, self).get_context_data(**kwargs) context['tasks'] = task.objects.filter(list=self.object) return context
however, don't have override get_context_data
method @ all. in template, can follow relationship backwards project tasks:
{% task in object.task_set.all %} <li>{{task}}</li> {% endfor %}
Comments
Post a Comment