Django Viewing post by categories

Viewed 39

I am new to DJango and still understanding how views and urls work. I am trying to show a list of categories in my side bar as well as get bread crumbs to work. I cannot get the breadcrumbs to show correctly unless I go to mysite/category/categoryItem/sub_categoryItem but then I cannot get it to pull up the post even though it list it in the list. It is just a blank page. This at least tells me that the template is pulling the sidebar data.

views.py
def show_category(request,hierarchy= None):
    category_slug = hierarchy.split('/')
    category_queryset = list(Category.objects.all())
    all_slugs = [ x.slug for x in category_queryset ]
    parent = None
    for slug in category_slug:
        if slug in all_slugs:
            parent = get_object_or_404(Category,slug=slug,parent=parent)
        else:
            instance = get_object_or_404(Post, slug=slug)
            breadcrumbs_link = instance.get_cat_list()
            category_name = [' '.join(i.split('/')[-1].split('-')) for i in breadcrumbs_link]
            breadcrumbs = zip(breadcrumbs_link, category_name)
            return render(request, "wiki/post_detail.html", {'instance':instance,'breadcrumbs':breadcrumbs})

    return render(request,"sidebar.html",{'post_set':parent.post_set.all(),'sub_categories':parent.children.all()})

def post_detail(request,slug=None):
    post = get_object_or_404(Post,slug=slug)
    return render(request,"wiki/post_detail.html",{ 'post':post})

def post_list(request, slug=None):
    posts = Post.objects.all()
    return render(request,"wiki/postlist.html",{ 'posts':posts})`enter code here`

urls.py

urlpatterns = [
    url(r'^wiki/$',  views.post_list , name='wiki'),
    url(r'^(?P<slug>[\w-]+)/$', views.post_detail, name="postdetail"),
    url(r'^category/(?P<hierarchy>.+)/$', views.show_category, name='category'),
]
0 Answers
Related