Middleware: 'WSGIRequest' object has no attribute 'get' - python

I try to access the Ip address using middleware request, Here is my sample code,
def process_request(self, request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[-1].strip()
else:
ip = request.META.get('REMOTE_ADDR') --> I am getting the error here
return ip
Here is my complete error trace:
Environment:
Request Method: GET
Request URL: http://localhost/admin/management/banipaddress/
Django Version: 1.6.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'utils',
'catalog',
'preview',
'cart',
'management')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'custom.middleware.banipmiddleware.BanIpMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/user/prodjango-env/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
201. response = middleware_method(request, response)
File "/home/user/prodjango-env/venv/local/lib/python2.7/site-packages/django/middleware/clickjacking.py" in process_response
30. if response.get('X-Frame-Options', None) is not None:
Exception Type: AttributeError at /admin/management/banipaddress/
Exception Value: 'WSGIRequest' object has no attribute 'get'

Error seems to be at another line
File "/home/user/prodjango-env/venv/local/lib/python2.7/site-packages/django/middleware/clickjacking.py" in process_response
30. if response.get('X-Frame-Options', None) is not None:
in clickjacking.py.
Don't know its yours. There is trying to access response.get() which is not correct.

Related

django format_html not working with string_concat

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

Genode Django App No Longer Serving Properly

My Geocode application which is based on Django is no longer serving properly, and I am getting the error detailed as follows:
Environment:
Request Method: GET
Request URL: http://52.36.3.179/
Django Version: 1.6.11
Python Version: 2.7.6
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.sitemaps',
'django.contrib.staticfiles',
'django.contrib.messages',
'django.contrib.humanize',
'django.contrib.gis',
'pagination',
'taggit',
'friendlytagloader',
'geoexplorer',
'leaflet',
'django_extensions',
'autocomplete_light',
'mptt',
'modeltranslation',
'djcelery',
'pinax_theme_bootstrap_account',
'pinax_theme_bootstrap',
'django_forms_bootstrap',
'account',
'avatar',
'dialogos',
'agon_ratings',
'announcements',
'actstream',
'user_messages',
'tastypie',
'polymorphic',
'guardian',
'geonode.people',
'geonode.base',
'geonode.layers',
'geonode.maps',
'geonode.proxy',
'geonode.security',
'geonode.social',
'geonode.catalogue',
'geonode.documents',
'geonode.api',
'geonode.groups',
'geonode.services',
'geonode.geoserver',
'geonode.upload',
'geonode.tasks')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.locale.LocaleMiddleware',
'pagination.middleware.PaginationMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
137. response = response.render()
File "/usr/lib/python2.7/dist-packages/django/template/response.py" in render
105. self.content = self.rendered_content
File "/usr/lib/python2.7/dist-packages/django/template/response.py" in rendered_content
81. context = self.resolve_context(self.context_data)
File "/usr/lib/python2.7/dist-packages/django/template/response.py" in resolve_context
159. return RequestContext(self._request, context, current_app=self._current_app)
File "/usr/lib/python2.7/dist-packages/django/template/context.py" in __init__
169. self.update(processor(request))
File "/usr/local/lib/python2.7/dist-packages/geonode/context_processors.py" in resource_urls
28. site = Site.objects.get_current()
File "/usr/lib/python2.7/dist-packages/django/contrib/sites/models.py" in get_current
47. current_site = self.get(pk=sid)
File "/usr/lib/python2.7/dist-packages/django/db/models/manager.py" in get
151. return self.get_queryset().get(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/django/db/models/query.py" in get
310. self.model._meta.object_name)
Exception Type: DoesNotExist at /
Exception Value: Site matching query does not exist.
I can provide more configuration settings if need be. Please don't hesitate to ask. I am trying to not have to reinstall the application. This problem happened after I reloaded the server in order to update the correct URL.

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',

Django Attribute Error: 'function' object has no attribute 'is_usable'

Django Attribute Error: 'function' object has no attribute 'is_usable'
I am setting up a Django blog. I followed these instructions and I am getting the following error:
Environment:
Request Method: GET
Request URL: http://url.com/
Django Version: 1.6.2
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'divinare',
'rest_framework',
'django_facebook',
'django.contrib.auth')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
139. response = response.render()
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/response.py" in render
105. self.content = self.rendered_content
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/response.py" in rendered_content
80. template = self.resolve_template(self.template_name)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/response.py" in resolve_template
56. return loader.select_template(template)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/loader.py" in select_template
180. return get_template(template_name)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/loader.py" in get_template
138. template, origin = find_template(template_name)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/loader.py" in find_template
121. loader = find_template_loader(loader_name)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/loader.py" in find_template_loader
104. if not func.is_usable:
Exception Type: AttributeError at /
Exception Value: 'function' object has no attribute 'is_usable'
Middleware:
MIDDLEWARE_CLASSES = (
'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',
)
Template Loaders:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.contrib.auth.context_processors.auth',
# 'django.template.loaders.eggs.Loader',
)
Any insights to what is causing the error would be very helpful.
Your problem is almost certainly that you have "django.contrib.auth.context_processors.auth" in your TEMPLATE_LOADERS setting. As the name implies, that's a context processor, not a template loader - it should go in TEMPLATE_CONTEXT_PROCESSORS.

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