不支持的格式字符'_'(0x5f)在索引1处
我正在尝试在 Django 的基于类的视图中获取动态类名。
这是我的类。
class ProductDetailView(TemplateView):
template_name = "%_%_details.html"
def get_template_names(self,tmp_name,tmp_name2):
return [self.template_name % tmp_name,tmp_name2]
def get_context_data(self, **kwargs):
context = super(ProductDetailView, self).get_context_data(**kwargs)
platform = self.request.GET.get('platform')
if platform == "AMAZON":
asin = self.request.GET.get('asin')
if asin:
#products = amazon.search(Keywords=q, SearchIndex='All')
products= amazon.lookup(ItemId=asin)
template_name = self.get_template_names('amazon','product')
context['products'] = products
我想访问模板 amazon_product_details.html。
但是在访问上面的视图时,我遇到了一个错误:unsupported format character '_' (0x5f) at index 1
。
我也尝试过使用 urlib 进行编码,但那也没有用。
请告诉我,我可能哪里做错了。
1 个回答
12
看起来你想用标准的C语言风格来插入字符串,但你缺少了真正的格式化字符。
template_name = "%s_%s_details.html"