Django manage.py test fails (django.contrib.messages.middleware.MessageMiddleware) - python

I get this over and over again after running manage.py test:
ERROR 4280 140735184636256 base: Internal Server Error: /add/error/
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py", line 89, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/messages/tests/urls.py", line 30, in add
getattr(messages, message_type)(request, msg)
File "/Library/Python/2.7/site-packages/django/contrib/messages/api.py", line 102, in error
fail_silently=fail_silently)
File "/Library/Python/2.7/site-packages/django/contrib/messages/api.py", line 22, in add_message
raise MessageFailure('You cannot add messages without installing '
MessageFailure: You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware
MessageMiddleware is enabled in my settings.py so not sure why. Any clue?

You are running all defined tests, including django's own tests. Specify the modules that you want to test:
python manage.py test my_app my_other_app

Related

Unable to debug or use pdb in Django: bdb.BdbQuit

I'm using Django (2, 2, 4, 'final', 0) within a docker, but I'm able to bash inside to open or execute whatever is required. But I can't debug. (How to debug in Django, the good way? states some methods, none work for me)
Within my views.py I'm having various functions, for instance this here.
def visGraph(request):
showgraph = 'Graphen'
selectDB = request.GET.get('selectDB', '')
__import__("pdb").set_trace()
title += " <i>"+showgraph+"</i> ("+selectDB+")"
It works fine until I fill in the pdb, adding the debugger makes my app crash immediately:
> /code/DjangoGraphen/views.py(74)visGraph()
-> title += " <i>"+showgraph+"</i> ("+selectDB+")"
(Pdb)
Internal Server Error: /DjangoGraphen/visGraph
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./DjangoGraphen/views.py", line 74, in visGraph
title += " <i>"+showgraph+"</i> ("+selectDB+")"
File "./DjangoGraphen/views.py", line 74, in visGraph
title += " <i>"+showgraph+"</i> ("+selectDB+")"
File "/usr/lib64/python3.7/bdb.py", line 88, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/lib64/python3.7/bdb.py", line 113, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
ERROR:django.request:Internal Server Error: /DjangoGraphen/visGraph
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./DjangoGraphen/views.py", line 74, in visGraph
title += " <i>"+showgraph+"</i> ("+selectDB+")"
File "./DjangoGraphen/views.py", line 74, in visGraph
title += " <i>"+showgraph+"</i> ("+selectDB+")"
File "/usr/lib64/python3.7/bdb.py", line 88, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/lib64/python3.7/bdb.py", line 113, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
[21/Oct/2019 17:47:14] "GET /DjangoGraphen/visGraph?selectDB=Test&showgraph=graph HTTP/1.1" 500 88178
It doesn't matter very much if I'm using __import__("pdb").set_trace() or breakpoint() - both return the same result.
In my settings.py I have so far DEBUG = True, setting it to False doesn't change anything.
I'm viewing the logs within my command line using:
docker logs django_web_1 -f
I assume for the pdb I require an active shell rather than just a log-viewer, but I can't figure out what to change or how to do that.
But tried already what's given here as an answer: Interactive shell in Django
But it just opens a Python-Shell.
The solution is actually rather easy. The problem is the docker. The solution is stated here and works:
https://medium.com/#vladyslav.krylasov/how-to-use-pdb-inside-a-docker-container-eeb230de4d11
Add this to your docker-compose.yml:
ports:
- "4444:4444"
stdin_open: true
tty: true
Install the remote-pdb and use instead of your default pdb command:
__import__("remote_pdb").set_trace(host='0.0.0.0', port=4444)
Log into your docker and telnet into the pdb session:
telnet 0.0.0.0 4444
If you know you are going to end up in the debugger you can use run instead of up.
$ docker-compose run --rm --service-ports django_web
The problem is that up assumes it will run multiple services, even when you tell it to only run one, so it will wrap them for you. That's also why it prepends the output with the service name:
web_1 | Some output
db_1 | Some db logs
The run command doesn't do this and so you can have a shell and a debugger without problems or remote_pdb workarounds.
NOTE: when using run, you have to configure the dependencies because not everything is automatically started otherwise.

django allauth social authentication retrieved email matches an existing user's email?

This is my first post in Stack exchange, I am Python and Django Noob trying to develop an application. The application uses django-registration for user registration and then I started plugging in 'social' authentication. I chose allauth as it has the ability to perform authentication among'st other things.
I have hit the same problem that was presented in this thread: django allauth facebook redirects to signup when retrieved email matches an existing user's email?
user login/signup failing if the user with same email address already exists in the database(due to registration with local registration path). I tried the solution provided in the above post and have issues. Looking for some help and advise here.
I have exactly the same code as above in my socialadapter.py under the following path
myproject/allauth/socialaccount/socialadapter.py
I have the following in my settings
LOGIN_URL = '/'
#LOGIN_REDIRECT_URL = '/'
LOGIN_REDIRECT_URL = "/users/{id}/mytags"
SOCIALACCOUNT_QUERY_EMAIL = True
ACCOUNT_AUTHENTICATION_METHOD='username_email'
SOCIALACCOUNT_EMAIL_REQUIRED = False
#create and use specific adapter to handle the issue reported here
# https://github.com/pennersr/django-allauth/issues/418
ACCOUNT_ADAPTER = "myproject.allauth.socialaccount.MyLoginAccountAdapter"
SOCIALACCOUNT_ADAPTER = 'myproject.allauth.socialaccount.MySocialAccountAdapter'
On starting the runserver and accessing the facebook/login , I see the following issue
[17/Jul/2014 11:49:43] "GET /myproject/accounts2/facebook/login/ HTTP/1.1" 500 59
---------------------------------------- Exception happened during processing of request from ('127.0.0.1', 58805) Traceback (most recent call last): File "C:\Python27\Lib\SocketServer.py", line 593, in process_request_thread
self.finish_request(request, client_address) File "C:\Python27\Lib\SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self) File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 139, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs) File "C:\Python27\Lib\SocketServer.py", line 651, in __init__
self.finish() File "C:\Python27\Lib\SocketServer.py", line 710, in finish
self.wfile.close() File "C:\Python27\Lib\socket.py", line 279, in close
self.flush() File "C:\Python27\Lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size]) error: [Errno 10053] An established connection was aborted by the software in your host machine
---------------------------------------- ERROR:django.request:Internal Server Error: /myproject/accounts2/facebook/login/callback/ Traceback (most recent call last): File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 109, in get_response
response = callback(request, *callback_args, **callback_kwargs) File "myproject\allauth\socialaccount\providers\oauth2\views.py", line 51, in view
return self.dispatch(request, *args, **kwargs) File "myproject\allauth\socialaccount\providers\oauth2\views.py", line 98, in dispatch
response=access_token) File "myproject\allauth\socialaccount\providers\facebook\views.py", line 43, in complete_login
return fb_complete_login(request, app, access_token) File "myproject\allauth\socialaccount\providers\facebook\views.py", line 31, in fb_complete_login
.sociallogin_from_response(request, extra_data) File "myproject\allauth\socialaccount\providers\base.py", line 44, in sociallogin_from_response
adapter = get_adapter() File "myproject\allauth\socialaccount\adapter.py", line 150, in get_adapter
return import_attribute(app_settings.ADAPTER)() File "myproject\allauth\utils.py", line 97, in import_attribute
ret = getattr(importlib.import_module(pkg), attr) AttributeError: 'module' object has no attribute 'MySocialAccountAdapter' [17/Jul/2014 11:49:46] "GET /myproject/accounts2/facebook/login/callback/?code=AQBShGWTHnGVvlo-fOVW7xjF9RUJo-k7P23zISHC70p aAR5uWYpnI46gpHFUCC5Rz-SviDyTITVRAUkZ-DhkZaHyBT2n5UBhhSwkACgCKTTgPrFLAZFBQs05AEZ67xfk-wRlF47DSjT26bbDdUmc1ptfFxP3W4qS5Y6b5Yrj iLTI3RMScOEM0EKUQjNySyj4XSAVk6wj4HcAbCVxiVv5QaH63ayxyt5Y5jQ0AOH3zsCngPaqFNJArXseMS6wfqSc8yDwcwWZKo1nGhcNtA9Gy_bqZNiTZSjPJguhT lBwbmDAJ9SUNI8AS3yzC-AKDtD2_bo&state=441rn77wUuLH HTTP/1.1" 500 147978
Initially the socialadapter.py would not even compile,all others did compile even after deleting the .pyc
I referred to this thread: pycompile for python3.2
and force compile but I still see the issue
Any suggestions on what I might be doing wrong here is greatly appreciated.
thank you for your valuable time.
-km
EDIT:
Environment
Python 2.7.5
allauth: 0.17
Ok I figured out the issue , I had the settings entry for my adapter missing a the full path to the class. Now I am able to login using Facebook.
However I have another issue, I am trying to enable Linked in login for the same app and have the following entries in the settings
SOCIALACCOUNT_PROVIDERS = \
{'linkedin':{'SCOPE': [ 'r_emailaddress',
'r_fullprofile',
'r_emailaddress',
'r_contactinfo',
'r_network'],
'PROFILE_FIELDS':
[
'id',
'first-name',
'last-name',
'email-address',
'picture-url',
'public-profile-url',
'skills',
'headline',
'industry',
'num-connections',
'positions',
'interests',
'languages',
'certifications',
'educations',
'courses',
'three-current-positions',
'three-past-positions',
'recommendations-received',
'honors-awards'
]
},
'facebook': {'SCOPE': ['email', 'user_about_me', 'user_birthday',
'user_education_history','user_work_history',
'user_hometown',
'user_location',
'user_religion_politics','user_subscriptions',
'read_stream',
'read_insights',
'read_friendlists',
'user_likes',
'user_interests',
'user_groups'
],
'AUTH_PARAMS': {},
'METHOD': 'oauth2'
},
}
Now When I use the login page, I get the following error
[17/Jul/2014 22:26:28] "GET /myproject/accounts2/linkedin/login/?process=connect HTTP/1.1" 302 0
ERROR:django.request:Internal Server Error: /myproject/accounts2/linkedin/login/callback/
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 109, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "myproject\allauth\socialaccount\providers\oauth\views.py", line 35, in view
return self.dispatch(request, *args, **kwargs)
File "myproject\allauth\socialaccount\providers\oauth\views.py", line 90, in dispatch
return complete_social_login(request, login)
File "myproject\allauth\socialaccount\helpers.py", line 113, in complete_social_login
sociallogin=sociallogin)
File "C:\Python27\lib\site-packages\django\dispatch\dispatcher.py", line 172, in send
response = receiver(signal=self, sender=sender, **named)
File "myproject\allauth\socialaccount\socialadapter.py", line 50, in link_to_local_user
email_address = sociallogin.account.extra_data['email']
KeyError: 'email'
[17/Jul/2014 22:26:50] "GET /myproject/accounts2/linkedin/login/callback/?oauth_token=77--d223fb8b-168f-4260-b93c-1a6e5ff2
e1e1&oauth_verifier=52724 HTTP/1.1" 500 139897
I am not sure how to fix this because the SCOPE for LinkedIn has : 'email-address', how can I fix this issue as the Email Fields in LinkedIn Documentation also says email-address
LinkedIn fields
Any suggestions are appreciated.
I am sorry I do not know how to put a bounty on the question and I do not have enough to place for the question also.
TIA
-km

Django filebrowser configuration error

I cant get past the first step, setting it up.
This is my settings.py of the application:
MEDIA_URL = '/media/'
MEDIA_ROOT = '/home/django/ristoturisto/media'
FILEBROWSER_DIRECTORY = ''
STATIC_ROOT = '/home/django/ristoturisto/static'
STATIC_URL = '/static/'
FILEBROWSER_DIRECTORY = MEDIA_ROOT
The folders exist, i copied the name from pwd.
I did collect static, that worked.
But when i do:
python manage.py test filebrowser
I get the follwing error:
Creating Test for the FileBrowser site: filebrowser
Creating test database for alias 'default'...
...........F......ERemoving left-over tmp dir: /home/django/ristoturisto/media/tmp_test_0
======================================================================
ERROR: runTest (filebrowser.tests.sites.TestSite_filebrowser)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/filebrowser/tests/sites.py", line 236, in runTest
test_do_upload(self)
File "/usr/lib/python2.6/site-packages/filebrowser/tests/sites.py", line 99, in test_do_upload
response = test.c.post(url, data=f.read(), content_type='application/octet-stream', HTTP_X_REQUESTED_WITH='XMLHttpRequest', X_File_Name='testimage.jpg')
File "/usr/lib/python2.6/site-packages/django/test/client.py", line 449, in post
response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
File "/usr/lib/python2.6/site-packages/django/test/client.py", line 262, in post
return self.request(**r)
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.6/site-packages/django/contrib/admin/views/decorators.py", line 16, in _checklogin
return view_func(request, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/django/views/decorators/csrf.py", line 77, in wrapped_view
return view_func(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/filebrowser/sites.py", line 476, in _upload_file
file_already_exists = self.storage.exists(file_name)
File "/usr/lib/python2.6/site-packages/django/core/files/storage.py", line 230, in exists
return os.path.exists(self.path(name))
File "/usr/lib/python2.6/site-packages/django/core/files/storage.py", line 246, in path
raise SuspiciousOperation("Attempted access to '%s' denied." % name)
SuspiciousOperation: Attempted access to '/tmp_test_0/testimage.jpg' denied.
======================================================================
FAIL: test_directory (filebrowser.tests.settings.SettingsTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/filebrowser/tests/settings.py", line 31, in test_directory
self.assertEqual(os.path.basename(DIRECTORY), '')
AssertionError: 'media' != ''
----------------------------------------------------------------------
Ran 19 tests in 3.810s
FAILED (failures=1, errors=1)
Destroying test database for alias 'default'...
The variable MEDIA_ROOT must have trailing slash at the end.
Comment and one line below from django-filebrowser settings file
# DO NOT USE A SLASH AT THE BEGINNING, DO NOT FORGET THE TRAILING SLASH AT THE END.
DIRECTORY = getattr(settings, "FILEBROWSER_DIRECTORY", 'uploads/')
Two lines from settings test where script fail
# Check for trailing slash
self.assertEqual(os.path.basename(DIRECTORY), '')

Django 1.5 with psycopg2 throw random errors

I faced with some strange problem. When I run single worker for my django application? like this:
python manage.py run_gunicorn -w 1
python manage.py runserver
All requests served correctly, but if start multiple workers like:
python manage.py run_gunicorn -w 10
Django respond with error to (about) half of my requests:
2013-06-13 18:02:39 [10205] [ERROR] Error handling request
Traceback (most recent call last):
File "/home/mezhenin/venv/karma/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 130, in handle_request
for item in respiter:
File "/home/mezhenin/venv/karma/local/lib/python2.7/site-packages/raven/middleware.py", line 27, in __call__
iterable = self.application(environ, start_response)
File "/home/mezhenin/venv/karma/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
response = self.get_response(request)
File "/home/mezhenin/venv/karma/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 177, in get_response
signals.got_request_exception.send(sender=self.__class__, request=request)
File "/home/mezhenin/venv/karma/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 170, in send
response = receiver(signal=self, sender=sender, **named)
File "/home/mezhenin/venv/karma/local/lib/python2.7/site-packages/django/db/__init__.py", line 68, in _rollback_on_exception
transaction.rollback_unless_managed(using=conn)
File "/home/mezhenin/venv/karma/local/lib/python2.7/site-packages/django/db/transaction.py", line 143, in rollback_unless_managed
connection.rollback_unless_managed()
File "/home/mezhenin/venv/karma/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 232, in rollback_unless_managed
self._rollback()
File "/home/mezhenin/venv/karma/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 59, in _rollback
return self.connection.rollback()
OperationalError: no connection to the server
I tried sqlite3 as DB backend for Django and it works fine. Do someone have the same problems with psycopg2?
Django==1.5
gunicorn==0.17.2
psycopg2==2.5
Sorry for disturbing. Problem was not in psycopg/Django/gunicorn. I use Nose for testing purposes:
==== settings.py ====
INSTALLED_APPS = INSTALLED_APPS + ('django_nose',)
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = ['--with-doctest']
when removed this lines, random errors disappear.

Why do I get "AttributeError: 'unicode' object has no attribute 'user' " on some specify url only?

I'm using the #login_required decorator in my project since day one and it's working fine, but for some reason, I'm starting to get "
AttributeError: 'unicode' object has no attribute 'user' " on some specific urls (and those worked in the past).
Example : I am the website, logged, and then I click on link and I'm getting this error that usually is linked to the fact that there is no SessionMiddleware installed. But in my case, there is one since I am logged on the site and the page I am on also had a #login_required.
Any idea?
The url is definied as : (r'^accept/(?P<token>[a-zA-Z0-9_-]+)?$', 'accept'),
and the method as : #login_required
def accept(request,token): ...
The Traceback:
Traceback (most recent call last):
File "/Users/macbook/virtualenv/proj/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 674, in __call__
return self.application(environ, start_response)
File "/Users/macbook/virtualenv/proj/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
response = self.get_response(request)
File "/Users/macbook/virtualenv/proj/lib/python2.6/site-packages/django/core/handlers/base.py", line 141, in get_response
return self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/Users/macbook/virtualenv/proj/lib/python2.6/site-packages/django/core/handlers/base.py", line 165, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/Users/macbook/virtualenv/proj/lib/python2.6/site-packages/django/core/handlers/base.py", line 100, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/Users/macbook/virtualenv/proj/lib/python2.6/site-packages/django/contrib/auth/decorators.py", line 25, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/Users/macbook/dev/pycharm-projects/proj/match/views.py", line 33, in accept
return __process(token,callback)
File "/Users/macbook/virtualenv/proj/lib/python2.6/site-packages/django/contrib/auth/decorators.py", line 24, in _wrapped_view
if test_func(request.user):
AttributeError: 'unicode' object has no attribute 'user'`
The decorator was on a private method that doesn't have the request as a parameter. I removed that decorator (left there because of a refactoring and lack of test [bad me]).
Problem solved.
This can also happen if you call a decorated method from another method without providing a request parameter.

Categories

Resources