简体   繁体   English

如何在 Django 的包含 function 中添加“variable.html”而不是硬编码模板名称?

[英]How to add "variable.html" in include function of Django instead of hard coding the template name?

I want to render different HTML templates for each product detail view.我想为每个产品详细信息视图呈现不同的 HTML 模板。 I used this url pattern我用这个 url 模式

path('product-detail/<int:pk>', views.ProductDetailView.as_view(), name='product-detail'),

Here is the view for product detail view这是产品详细信息视图的视图

class ProductDetailView(View):
def get(self, request, pk):
    product = Product.objects.get(pk=pk)
    return render (request, 'esell/productdetail.html', {'product':product})

Here is the detail template这是详细模板

<h2>{{ product.title }}</h2>
 <hr>
 <p>Product Description: {{ product.description }}</p>

  

I want different detail templates to show for different product clicking on the product url.我希望点击产品 url 为不同的产品显示不同的详细信息模板。 I tried to name the different html templates based one their id name like this "1.html" and tried to add include function to render different templates based on their id name like this我试图命名不同的 html 模板,基于它们的 id 名称,如“1.html”,并尝试添加包含 function 以根据它们的 id 名称呈现不同的模板,如下所示

{% include '{{product.id}}.html' %}

But it's not working.但它不起作用。 Is there any way to add variable like that in include or other way to complete this task?有没有办法在包含或其他方式中添加类似的变量来完成这项任务?

You could do an {%if some_variable %} and then {%include 'xxx.html'%}你可以做一个{%if some_variable %}然后{%include 'xxx.html'%}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM