django include a template into another template

狂风中的少年 提交于 2020-05-16 03:27:06

问题


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 %}

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

views.py in posts app

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:


In your 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)

You should have something like this in your view.

In template:

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


来源:https://stackoverflow.com/questions/45860663/django-include-a-template-into-another-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!