django将模板包含到另一个模板中

时间:2017-08-24 11:40:17

标签: django templates django-templates

我正在尝试将模板包含到另一个模板中,但我现在遇到的问题是嵌入式模板在我尝试将其嵌入的新模板中不显示在其上呈现的内容。

配置文件应用程序中的

detail.html

{% include "list.html" with instance=user.posts_created.all %}

我在templates文件夹中有模板。可以看到模板,但内容未显示在detail.html

将根据要求提供其他代码,谢谢

发布应用

views.py

def axle(request , tag_slug=None):
    results = Post.objects.all().filter(draft=False)#.filter(publish__lte=timezone.now())
    #results = results.filter(user=request.user)
    tag = None
    if tag_slug:
        tag = get_object_or_404(Tag, slug=tag_slug)
        results = results.filter(tags__in=[tag])
    que = request.GET.get("q")
    if que:
        results =results.filter(
            Q(title__icontains=que)|
            Q(content__icontains=que)).distinct()
    paginator = Paginator(results, 4) # Show 25 contacts per page
    pages ="page"
    page = request.GET.get('page')
    try:
        query = paginator.page(page)
    except PageNotAnInteger:
        query = paginator.page(1)
    except EmptyPage:
        query = paginator.page(paginator.num_pages)

    context = {
        "objects": query,
        "pages": pages
    }
    template = 'list.html'
    return render(request,template,context)

1 个答案:

答案 0 :(得分:2)

view

def post(request):
    post_list = posts_created.objects.all() # or something which you want to do.
    context = {
    'post_list': post_list,
    }
    return render(request, 'detail.html', context)

您应该在view

中使用此类内容

template

{% include "app_name/list.html" with objects=post_list %}