43 def get_index_view_url(self):
44 """Get the url for the index view"""
45 content_type = ContentType.objects.get_for_model(self.__class__)
46 return reverse("%s:%s_list" % (content_type.app_label, content_type.model))
29 def get_index_view_url(self):
30 """Get the url for the index view"""
31 content_type = ContentType.objects.get_for_model(self.__class__)
32 return reverse("%s:index" % (content_type.app_label))33
34
35class Choice(auto_prefetch.Model):
77 as_varname = tokens[3]
78
79 if tokens[4] != "for":
80 raise TemplateSyntaxError("Fifth argument in %r tag must be 'for'" % tokens[0])81
82 content_types = _get_content_types(tokens[0], tokens[5:])
83 return GetLastElementsNode(count, as_varname, content_types)
72 raise TemplateSyntaxError("Second argument in %r tag must be a integer" % tokens[0])
73
74 if tokens[2] != "as":
75 raise TemplateSyntaxError("Third argument in %r tag must be 'as'" % tokens[0])76
77 as_varname = tokens[3]
78
69 try:
70 count = int(tokens[1])
71 except ValueError:
72 raise TemplateSyntaxError("Second argument in %r tag must be a integer" % tokens[0])73
74 if tokens[2] != "as":
75 raise TemplateSyntaxError("Third argument in %r tag must be 'as'" % tokens[0])
f-strings are the fastest way to format strings as compared to the following methods:
%
format()
str.join
+
operator to concatinate stringTemplate.substitute
Some less preferred ways to format strings are the following:
from string import Template
menu = ('eggs', 'spam', 42.4)
old_order = "%s and %s: %.2f ¤" % menu # [consider-using-f-string]
beginner_order = menu[0] + " and " + menu[1] + ": " + str(menu[2]) + " ¤"
joined_order = " and ".join(menu[:2])
format_order = "{} and {}: {:0.2f} ¤".format(menu[0], menu[1], menu[2])
named_format_order = "{eggs} and {spam}: {price:0.2f} ¤".format(eggs=menu[0], spam=menu[1], price=menu[2])
template_order = Template('$eggs and $spam: $price ¤').substitute(eggs=menu[0], spam=menu[1], price=menu[2])
Consider using f-strings as shown below:
menu = ('eggs', 'spam', 42.4)
f_string_order = f"{menu[0]} and {menu[1]}: {menu[2]:0.2f} ¤"