Issue
I am trying to display a two line error message to a user. All attempts I have made have failed and I cannot figure this out, despite searching SO & google.
The error I am getting is:
Exception Type: IndexError
Exception Value: tuple index out of range
The issue seems to be the use of the format_html()
with the string_concat()
.
Here is my python code:
from django.utils import translation
from django.utils.html import format_html
from django.utils.translation import string_concat
from django.utils.translation import ugettext_lazy as _
....
if not request.user.is_staff and language_versions_num_enabled > max_languages:
error_message = format_html(
string_concat(
_(u'You are only permitted <b>{}</b> <u>enabled</u> Language Versions for the selected <b>{}</b> subscription plan.'),
' <icon class="fa fa-frown-o icon_size26 icon_padding"></icon>',
'<br /><br />',
_(u'Either select a different subscription plan or disable <b>{}</b> of your <b>{}</b> <u>enabled</u> Language Versions.'.format(max_languages, stripe_plan['title'], language_versions_num_enabled-max_languages, language_versions_num_enabled))
)
)
messages.add_message(request, messages.ERROR, error_message)
else:
I am really hoping someone can point out what I am doing wrong here.
EDIT
I am using django 1.10 and python 3.5.2.
Here is the stack trace:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/subscription/subscription/
Django Version: 1.10.5
Python Version: 3.5.2
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.humanize',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.sitemaps',
'django.contrib.staticfiles',
'admin_honeypot',
'axes',
'rest_framework',
'rest_framework.authtoken',
'captcha',
'my_app.contact',
'djstripe',
'my_app.common',
'my_app.users',
'my_app.core',
'imagekit',
'parsley',
'registration',
'templatetag_handlebars',
'settings_context_processor',
'crispy_forms',
'lockdown',
'rosetta',
'raven.contrib.django.raven_compat')
Installed Middleware:
('raven.contrib.django.middleware.SentryMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'my_app.users.middleware.LocaleOverrideMiddleware',
'django.middleware.locale.LocaleMiddleware',
'my_app.users.middleware.AdminLocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'lockdown.middleware.LockdownMiddleware')
Traceback:
File "C:\Users\laptop\desktop\my_app\env3\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "C:\Users\laptop\desktop\my_app\env3\lib\site-packages\django\core\handlers\base.py" in _legacy_get_response
249. response = self._get_response(request)
File "C:\Users\laptop\desktop\my_app\env3\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Users\laptop\desktop\my_app\env3\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\laptop\desktop\my_app\env3\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "C:\Users\laptop\desktop\my_app\env3\lib\site-packages\django\views\decorators\http.py" in inner
40. return func(request, *args, **kwargs)
File "C:\Users\laptop\desktop\my_app\my_app\core\decorators.py" in wrapper
21. return f(request, *args, **kwargs)
File "C:\Users\laptop\desktop\my_app\my_app\core\views\user_settings_views.py" in subscription
402. _(u'Either select a different subscription plan or disable <b>{}</b> of your <b>{}</b> <u>enabled</u> Language Versions.'.format(max_languages, stripe_plan['title'], language_versions_num_enabled-max_languages, language_versions_num_enabled))
File "C:\Users\laptop\desktop\my_app\env3\lib\site-packages\django\utils\html.py" in format_html
106. return mark_safe(format_string.format(*args_safe, **kwargs_safe))
File "C:\Users\laptop\desktop\my_app\env3\lib\site-packages\django\utils\functional.py" in __wrapper__
112. return getattr(res, method_name)(*args, **kw)
Exception Type: IndexError at /subscription/subscription/
Exception Value: tuple index out of range
Solution
You put format in the wrong place. Try this:
error_message = format_html(
string_concat(
_(u'You are only permitted <b>{}</b> <u>enabled</u> Language Versions for the selected <b>{}</b> subscription plan.'),
' <icon class="fa fa-frown-o icon_size26 icon_padding"></icon>',
'<br /><br />',
_(u'Either select a different subscription plan or disable <b>{}</b> of your <b>{}</b> <u>enabled</u> Language Versions.')
).format(max_languages, stripe_plan['title'], language_versions_num_enabled-max_languages, language_versions_num_enabled)
)
Answered By - munsu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.