【发布时间】:2020-10-21 05:11:29
【问题描述】:
我是 django 概念的新手,所以我想在另一个 html 模板的 div 中包含一个 html 模板。我尝试使用 iframe,但是当我加载页面时它说 localhost 拒绝连接,然后我参考了一些堆栈答案,我用 include 做到了,但是当我给 include 时,页面没有加载。
IndexPage.html
<section id="testimonial" class="testimonial section-padding">
<div class="overlay"></div>
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-7 col-md-12 col-sm-12 col-xs-12">
<div id="testimonials" class="owl-carousel wow fadeInUp" data-wow-delay="1.2s">
<div class="item">
<div class="testimonial-item">
<div class="info">
<!-- <iframe id="encoder_iframe" height=75% width="50%" src='/templates/index.html'></iframe> -->
{% include "index.html" %}
</div>
</div>
</div>
</div>
</div>
</div>
</section>
index.html
{% extends 'base.html' %}
{% block content %}
<h1 class="page-header"></h1>
<p class="lead"></p>
<p class="lead"><a href="/products">Customer ListList</a>.</p>
{% endblock %}
views.py
def IndexPage(request):
return render(request,'IndexPage.html',{})
def index(request):
return render(request, 'index.html')
def product_list(request):
products = Product.objects.all()
return render(request, 'product_list.html', {'products': products})
def save_product_form(request, form, template_name):
data = dict()
if request.method == 'POST':
if form.is_valid():
form.save()
data['form_is_valid'] = True
products = Product.objects.all()
data['html_product_list'] = render_to_string('includes/partial_product_list.html', {
'products': products
})
else:
data['form_is_valid'] = False
context = {'form': form}
data['html_form'] = render_to_string(template_name, context, request=request)
return JsonResponse(data)
def product_create(request):
if request.method == 'POST':
form = ProductForm(request.POST)
else:
form = ProductForm()
return save_product_form(request, form, 'includes/partial_product_create.html')
def product_update(request, pk):
product = get_object_or_404(Product, pk=pk)
if request.method == 'POST':
form = ProductForm(request.POST, instance=product)
else:
form = ProductForm(instance=product)
return save_product_form(request, form, 'includes/partial_product_update.html')
def product_delete(request, pk):
product = get_object_or_404(Product, pk=pk)
data = dict()
if request.method == 'POST':
product.delete()
data['form_is_valid'] = True
products = Product.objects.all()
data['html_product_list'] = render_to_string('includes/partial_product_list.html', {
'products': products
})
else:
context = {'product': product}
data['html_form'] = render_to_string('includes/partial_product_delete.html', context, request=request)
return JsonResponse(data)
所以必须在 indexpage.html 的 div 中包含 index.html
【问题讨论】:
-
你想在 中加载什么?
-
我的产品列表将在此处显示