django format_html not working with string_concat - python

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

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)
)

Related

django upgrade from 1.9 to 1.10 __init__() got an unexpected keyword argument 'current_app'

ive just upgraded Django from 1.9 to 1.10. i run 1.10 in my test environment and ironed out any upgrade issues to make sure it all worked.
however when ive gone to do it on my live site and gone to load the home page i get the errors below
Environment:
Request Method: GET
Request URL: http://it.local.com/
Django Version: 1.10.5
Python Version: 2.7.5
Installed Applications:
('home.apps.HomeConfig',
'oncall.apps.OncallConfig',
'tools.apps.ToolsConfig',
'sites.apps.SitesConfig',
'maintenance.apps.MaintenanceConfig',
'service.apps.ServiceConfig',
'management.apps.ManagementConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'bootstrap_pagination',
'easy_thumbnails')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware')
Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/www/infternal/home/views.py" in index
13. return render(request, 'home/index.html', {"user": user})
File "/usr/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render
49. context_instance = RequestContext(request, current_app=current_app)
Exception Type: TypeError at /
Exception Value: __init__() got an unexpected keyword argument 'current_app'
It looks like you haven't installed Django 1.10 properly. The line from the traceback context_instance = RequestContext(request, current_app=current_app) does not exist in Django 1.10. I would try uninstalling and reinstalling Django, and then restarting the server.

django: Unexpected data type exception trying to use django-excel

So I am trying to use django-excel: https://github.com/pyexcel/django-excel
and just simply export a certain model into xls. I set up the settings according to the documentation, but I got this error when running:
Exception at /calbase/export/sheet
Unexpected data type <class 'django.db.models.fields.files.FieldFile'>
Here is my views:
def export_data(request, atype):
if atype == "sheet":
return excel.make_response_from_a_table(
Equipment, 'xls', file_name="sheet")
elif atype == "book":
return excel.make_response_from_tables(
[Equipment, Calibration], 'xls', file_name="book")
elif atype == "custom":
equipment = Equipment.objects.get(slug='ide')
query_sets = Choice.objects.filter(equipment=equipment)
column_names = ['choice_text', 'id', 'votes']
return excel.make_response_from_query_sets(
query_sets,
column_names,
'xls',
file_name="custom"
)
else:
return HttpResponseBadRequest(
"Bad request. please put one of these " +
"in your url suffix: sheet, book or custom")
and when I tried to go
http://127.0.0.1:8000/calbase/export/sheet
it gives me the exception above.
By the way is there any good example using django-excel? I can't seem to run the one provided
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/calbase/export/sheet
Django Version: 1.10
Python Version: 3.5.2
Installed Applications:
['calbase.apps.CalbaseConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'haystack',
'whoosh',
'carton']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\hansong.li\Documents\GitHub\equipCal\calbase\views.py" in export_data
97. Equipment, 'xls', file_name="sheet")
File "C:\Users\hansong.li\Documents\GitHub\equipCal\django_excel\__init__.py" in make_response_from_a_table
147. file_name=file_name, **keywords)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel_webio\__init__.py" in make_response
244. file_stream = pyexcel_instance.save_to_memory(file_type, None, **keywords)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel\sheets\sheet.py" in save_to_memory
114. self.save_to(out_source)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel\sheets\sheet.py" in save_to
79. source.write_data(self)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel\sources\file_source_output.py" in write_data
86. sheet, **self.keywords)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel\sources\renderer\_excel.py" in render_sheet_to_stream
35. **keywords)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel_io\io.py" in save_data
70. **keywords)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel_io\io.py" in store_data
91. writer.write(data)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel_io\book.py" in write
177. sheet_writer.write_array(incoming_dict[sheet_name])
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel_io\sheet.py" in write_array
82. self.write_row(row)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel_xls\xls.py" in write_row
230. self.native_sheet.write(self.current_row, i, value)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\xlwt\Worksheet.py" in write
1033. self.row(r).write(c, label, style)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\xlwt\Row.py" in write
263. raise Exception("Unexpected data type %r" % type(label))
Exception Type: Exception at /calbase/export/sheet
Exception Value: Unexpected data type <class 'django.db.models.fields.files.FieldFile'>

Django - Autocomplete_Light's "Add Another" popup declares: "'initial' is an invalid keyword argument for this function"

I am working on getting the "add another" popup to work with django-autocomplete_light.
Following along in the docs:
http://django-autocomplete-light.readthedocs.org/en/latest/addanother.html
I have set up my URLs:
import autocomplete_light.shortcuts as al
from AlmondKing.FinancialLogs import models
from AlmondKing.FinancialLogs import forms
urlpatterns = [
url(r'^branches/autocreate/$', al.CreateView.as_view(
model=models.CompanyBranch, form_class=forms.CompanyBranch),
name='branch_autocreate'),
]
and my autocomplete_light_registry.py
al.register(CompanyBranch,
search_fields=['^branch_name'],
attrs={
'placeholder': 'Branch',
'data-autocomplete-minimum-characters': 1,
},
widget_attrs={
'data-widget-maximum-values': 1,
'class': 'modern-style',
},
add_another_url_name='company:branch_autocreate',
)
However, when I click the plus sign to add a new related object, I get the following error:
TypeError at /company/branches/autocreate/
'initial' is an invalid keyword argument for this function
I've been trying to find a way to do this for a while and I'm so close!
Now, I am hoping someone can read the traceback and help me understand what went wrong:
Environment:
Request Method: GET
Request URL: http://localhost:8000/company/branches/autocreate/?_popup=1&winName=id_branch
Django Version: 1.8.2
Python Version: 3.4.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'AlmondKing.InventoryLogs',
'AlmondKing.FinancialLogs',
'AlmondKing.AKGenius',
'autocomplete_light')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'AlmondKing.AKGenius.middleware.RequireLoginMiddleware')
Traceback:
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\views\generic\edit.py" in get
245. return super(BaseCreateView, self).get(request, *args, **kwargs)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\views\generic\edit.py" in get
205. form = self.get_form()
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\views\generic\edit.py" in get_form
74. return form_class(**self.get_form_kwargs())
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\base.py" in __init__
480. raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
Exception Type: TypeError at /company/branches/autocreate/
Exception Value: 'initial' is an invalid keyword argument for this function
Try this:
add_another_url_name='company:branch_autocreate',
add_another_url_name='branch_autocreate',

python-django file upload 'key error'

I am using python 3.2 & django 1.6. I want to upload to upload a 'csv' file and read data inorder to insert it into database(postgresql). Here is my code
Template
<form id = "ListForm" name = "ListForm" action = "" method = 'POST' enctype="multipart/form-data">
<table>
<tr>
<td>PM List</td>
<td><input type="file" name="file_pm" id="file_pm" ></td>
</tr>
<tr><td><input type="submit" value="Upload" name="pmUpload" id="pmUpload" class="button"></td></tr>
</table>
</form>
Python code is:
import cgi
def pmUpload(request):
form = cgi.FieldStorage()
fileitem = form["file_pm"]
I got 'KeyError' in the lastline of the above python code.I was trying to read data one by one.
And the full traceback error is
Environment:
Request Method: POST
Request URL: http://MYURL/cc/pmList/
Django Version: 1.6.5
Python Version: 3.2.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ccApp')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/env/ccENV/lib/python3.2/site-packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/env/ccENV/lib/python3.2/site-packages/django/views/decorators/csrf.py" in wrapped_view
57. return view_func(*args, **kwargs)
File "/home/env/ccENV/lib/python3.2/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "/home/env/cccENV/ccc/ccApp/pmList.py" in pmUpload
19. fileitem = form["file_pm"]
File "/usr/lib/python3.2/cgi.py" in __getitem__
575. raise KeyError(key)
Exception Type: KeyError at /cc/pmList/
Exception Value: 'file_pm'
How to read data in python 3.2 from the uploaded file(data.csv)
The file should be in request.FILES['file_pm'].

django-avatar combined with django-notification gives an error

I have installed django-avatar and django-notification (django-avatar uses django-notification, if the latter is installed, in order to send necessary messages to user).
However when I upload/change/delete my avatar I get this error:
Request Method: POST
Request URL: http://localhost:8000/avatar/change/
Django Version: 1.3
Python Version: 2.6.4
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'grappelli',
'filebrowser',
'tinymce',
'south',
'avatar',
'django.contrib.admin',
'notification',
'tipleaders.main']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "C:\Tools\Python26\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Tools\Python26\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "C:\Tools\Python26\lib\site-packages\django_avatar-1.0.5-py2.6.egg\avatar\views.py" in change
77. notification.send([request.user], "avatar_updated", {"user": request.user, "avatar": avatar})
File "C:\Tools\Python26\lib\site-packages\django_notification-0.2-py2.6.egg\notification\models.py" in send
351. return send_now(*args, **kwargs)
File "C:\Tools\Python26\lib\site-packages\django_notification-0.2-py2.6.egg\notification\models.py" in send_now
276. reverse("notification_notices"),
File "C:\Tools\Python26\lib\site-packages\django\core\urlresolvers.py" in reverse
391. *args, **kwargs)))
File "C:\Tools\Python26\lib\site-packages\django\core\urlresolvers.py" in reverse
337. "arguments '%s' not found." % (lookup_view_s, args, kwargs))
Exception Type: NoReverseMatch at /avatar/change/
Exception Value: Reverse for 'notification_notices' with arguments '()' and keyword arguments '{}' not found.
django-avatar worked neatly just before django-notification was installed.
What went wrong?
URL notification_notices does not exist. Install django-notification urls, add to urls.py:
url('notification/', include('notification.urls')),
I recommend that you review your installation of django-notification.

Categories