Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unsupported format character '_' (0x5f) at index 1

Tags:

python

django

I am trying to get the dynamic class name in django class based view .

Here is my class .

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

I want to accees the template amazon_product_details.html .

I am getting the error unsupported format character '_' (0x5f) at index 1 while accessing the above view .

I tried urlib quoting also but that also doesn't work .

Please do let me know what might I am doing wrong here .

like image 886
Nags Avatar asked Jul 07 '14 14:07

Nags


1 Answers

Looks like you're trying to use standard C-style string interpolation, but you're missing the actual formatting characters.

template_name = "%s_%s_details.html"
like image 139
Daniel Roseman Avatar answered Sep 30 '22 15:09

Daniel Roseman