python - Building cascade structure -
i want build cascade structure category-subject-post
in project. here code:
models.py:
class category(models.model): category_name = models.charfield(max_length=60, unique=true) slug_category = models.slugfield(unique=true) def get_absolute_url(self): return reverse("skill:subjects", kwargs={"slug_category": self.slug_category}) class subject(models.model): category = models.foreignkey(category) subject_name = models.charfield(max_length=100, unique=true) slug_subject = models.slugfield(unique=true) def get_absolute_url(self): return reverse("skill:subject", kwargs={ "slug_category": self.category.slug_category, "slug_subject": self.slug_subject }) class post(models.model): subject = models.foreignkey(subject) ...
views.py:
def index(request): context = { 'category_it': category.objects.filter(class_name='it'), 'category_ps': category.objects.filter(class_name='ps'), 'category_ac': category.objects.filter(class_name='ac'), 'category_tc': category.objects.filter(class_name='tc'), 'category_sc': category.objects.filter(class_name='sc'), 'category_ss': category.objects.filter(class_name='ss'), } return render(request, 'skill/index.html', context) class subjectsview(generic.listview): model = subject queryset = subject.objects.all() template_name = "skill/subjects.html" class subjectview(generic.listview): model = post queryset = post.objects.all() template_name = 'skill/subject.html'
urls.py:
url(r'^$', views.index, name='index'), # /programming url(r'^(?p<slug_category>[\w-]+)/$', views.subjectsview.as_view(), name='subjects'), # /programming/git url(r'^(?p<slug_category>[\w-]+)/(?p<slug_subject>[\w-]+)/$', views.subjectview.as_view(), name='subject'), # /programming/git/43121 url(r'^(?p<slug_category>[\w-]+)/(?p<slug_subject>[\w-]+)/(?p<pk>[0-9]+)/$', views.post, name='post'), ]
templates:
index.html:
{% category in category_it %} <a href="{% url 'skill:subjects' category.slug_category %}" class="item">{{ category }}</a> {% endfor %}
subjects.html:
{% object in object_list %} <a href="{{ object.get_absolute_url }}" class="item"><h4>{{ object }}</h4></a> {% endfor %}
i don't know query need , where(views or template), tried , hardcoded (index in views.py , template 6times larger) can't afford on next step because of scope.
i need categories in index.html, list of subjects belonging particular category in subjects.html , posts belonging subject in subject.html. can please me finding working solution
this site invaluable work way around class based views (listview)
i think need override get
method, this:
class subjectsview(generic.listview): ... def get(self, request, *args, slug_category=none, **kwargs): if slug_category: self.queryset = subject.objects.filter(category__slug_category=slug_category) super(subjectsview, self).get(request, *args, slug_category=slug_category, **kwargs)
Comments
Post a Comment