问题
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