【问题标题】:load content of one HTML template on div of another HTML template using DJANGO使用 DJANGO 在另一个 HTML 模板的 div 上加载一个 HTML 模板的内容
【发布时间】: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

structure of folder

【问题讨论】:

标签: python django iframe


【解决方案1】:

可能,Django 不知道从哪里获取 index.html。您是否将 {% load static %} 放在 IndexPage.html 的顶部?

【讨论】:

    【解决方案2】:

    您是否正在将 product_list 加载到“/products”?

    将“/products”更改为“{% url 'product_list' %} 可能会有所帮助。

    【讨论】:

    • 当我提供类似的东西时,它只会提供加载小部件,不会加载任何内容
    猜你喜欢
    • 2014-05-29
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2020-10-31
    • 2011-05-28
    • 1970-01-01
    相关资源
    最近更新 更多