django将一个模板包含到另一个模板中 [英] django include a template into another template

查看:115
本文介绍了django将一个模板包含到另一个模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to include a template into another template but the problem I am having now is the embedded templates doesnt display contents rendered on it in the new template I am trying to embed it into.

detail.html in profile application

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

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

and I have the template in the templates folder. The template is seen but the content does not show up in the detail.html

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

additional codes would be provided on request thanks

posts应用中的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)


推荐答案

在您的视图 c中:

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)

在您的视图中应该有类似的东西

模板

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

这篇关于django将一个模板包含到另一个模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆