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

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.

Related

Unique PermissionError: [WinError 5] Access is denied IIS Issue

I am currently setting up a django site to run on IIS, however I am running into an issue with the USZipCodes python module, When it goes to call the USZipCodes module if gets an access denied error.
I have confirmed all folders regarding this EXCEPT the systemprofile folder has proper permissions for the IIS_IUSRS profile. I would do the systemprofile as well but it just give me more errors and I would prefer not to
This module works fine when not IIS, through CMD it works fine, it is only when using IIS to run the app does this occur.
I suspect I may just have the cut the module out but I would prefer not too
Error Log:
Traceback (most recent call last):
File "C:\Python310\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Python310\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\inetpub\wwwroot\Apps\scituateApp\.\tickets\views.py", line 286, in push
json_client['street_name'], zco(json_client['zip']), json_client['state'], json_client['zip'],
File "C:\inetpub\wwwroot\Apps\scituateApp\.\tickets\views.py", line 15, in zco
search = SearchEngine()
File "C:\Python310\lib\site-packages\uszipcode\search.py", line 153, in __init__
self._download_db_file_if_not_exists()
File "C:\Python310\lib\site-packages\uszipcode\search.py", line 173, in _download_db_file_if_not_exists
download_db_file(
File "C:\Python310\lib\site-packages\uszipcode\db.py", line 33, in download_db_file
Path(db_file_path).parent.mkdir(parents=True, exist_ok=True)
File "C:\Python310\lib\site-packages\pathlib_mate\pathlib2.py", line 1614, in mkdir
_try_except_filenotfounderror(_try_func, _exc_func)
File "C:\Python310\lib\site-packages\pathlib_mate\pathlib2.py", line 117, in _try_except_filenotfounderror
try_func()
File "C:\Python310\lib\site-packages\pathlib_mate\pathlib2.py", line 1605, in _try_func
self._accessor.mkdir(self, mode)
File "C:\Python310\lib\site-packages\pathlib_mate\pathlib2.py", line 593, in wrapped
return strfunc(str(pathobj), *args)
PermissionError: [WinError 5] Access is denied: 'C:\\Windows\\system32\\config\\systemprofile\\.uszipcode'
Figured I would follow up. After quite some hours I was able to resolve the issue.
When you setup the IIS, it creates a local group called IIS_LUSRS. You must add the user (local or domain) that you log into to run IIS to this local groups (can be done through the lusrmgr.msc program). Then after this is done, allow that group modify and write privs on the python site package pathlib and the system profile in system 32.
This is not the best solutions, but it fixed it for me...

Django test throwing an error on response status code 404

I'm following along with a lecture on django testing and this is one of the tests:
def test_invalid_flight_page(self):
max_id = Flight.objects.all().aggregate(Max("id"))["id__max"]
c = Client()
response = c.get(f"/flights/{max_id + 1}")
self.assertEqual(response.status_code, 404)
When I run manage.py tests it throws an error on this test, essentially saying there is no matching flight:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.......E..
======================================================================
ERROR: test_invalid_flight_page (flights.tests.FlightTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\sarah\Desktop\airline\flights\tests.py", line 68, in test_invalid_flight_page
response = c.get(f"/flights/{max_id + 1}")
File "C:\Python\Python385\lib\site-packages\django\test\client.py", line 732, in get
response = super().get(path, data=data, secure=secure, **extra)
File "C:\Python\Python385\lib\site-packages\django\test\client.py", line 393, in get
return self.generic('GET', path, secure=secure, **{
File "C:\Python\Python385\lib\site-packages\django\test\client.py", line 470, in generic
return self.request(**r)
File "C:\Python\Python385\lib\site-packages\django\test\client.py", line 709, in request
self.check_exception(response)
File "C:\Python\Python385\lib\site-packages\django\test\client.py", line 571, in check_exception
raise exc_value
File "C:\Python\Python385\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Python\Python385\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\sarah\Desktop\airline\flights\views.py", line 21, in flight
flight = Flight.objects.get(pk=flight_id)
File "C:\Python\Python385\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python\Python385\lib\site-packages\django\db\models\query.py", line 429, in get
raise self.model.DoesNotExist(
flights.models.Flight.DoesNotExist: Flight matching query does not exist.
----------------------------------------------------------------------
Ran 10 tests in 0.120s
FAILED (errors=1)
Destroying test database for alias 'default'...
But that is the point, there is no flight with that id so the response status code for that request should equal 404. As far as I can see I have copied the code accurately from the lecture but the lecturer's tests are all running okay. Can anyone see what I may be missing?
When I change the expected response.status_code to 200 self.assertEqual(response.status_code, 200) it gives me the same error so this indicates to me that the main problem lies with the response line?
Please let me know if there is anything more you need to see.
Your view class is not handling when the query resolves to nothing. In django it's done as follows:
try:
result = SomeModel.objects.get(pk=some_id)
except SomeModel.DoesNotExist:
# Return 404 here
I've met the same problem. Indeed, it came from the response line or client.get() method itself. In my case, I add "/" at the end of the quotation mark as:
response = c.get(f"/flights/{max_id + 1}/")
And then check assert condition. Now it functions as I want.
Also found that:
If you substitute a word instead of a number as an invalid page, it works just fine. For example:
THIS ONE OK (404 were detected):
response = c.get(f"/flights/whatever")
THIS ONE DOESN'T WORK (404 were not detected and errors were alerted):
response = c.get(f"flights/15")

unable to load static css file in the dash app

I have built a single page dash app which runs as expected when run as a single file, but when I am trying to run it as a whole app, the CSS does not load correctly.
Below is my folder structure
While I load the whole app by manage.py below is the error that I get
Internal Server Error: /assets/internal.css
Traceback (most recent call last):
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\my_project\analyser_tool\views.py", line 32, in dash_index
return HttpResponse(dispatcher(request))
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\my_project\analyser_tool\views.py", line 27, in dispatcher
return response.get_data()
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\werkzeug\wrappers.py", line 986, in get_data
self._ensure_sequence()
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\werkzeug\wrappers.py", line 1035, in _ensure_sequence
raise RuntimeError('Attempted implicit sequence conversion '
RuntimeError: Attempted implicit sequence conversion but the response object is in direct passthrough mode.
This is my only second dash app that I am working and unfortunately does not have much experience in it. I am really hopping if some could please help me fix this issue. I have been struggling with it for couple of days now.
Thanks a lot in advance !!
With continued research, I was able to resolve the issue by adding the following to my server.py
css_directory = os.getcwd()
stylesheets = ['stylesheet.css']
static_css_route = '/static/'
#app.server.route('{}<stylesheet>'.format(static_css_route))
def serve_stylesheet(stylesheet):
if stylesheet not in stylesheets:
raise Exception(
'"{}" is excluded from the allowed static files'.format(
stylesheet
)
)
return flask.send_from_directory(css_directory, stylesheet)
for stylesheet in stylesheets:
app.css.append_css({"external_url": "/static/{}".format(stylesheet)})
This answer was answered before and is taken from
https://community.plot.ly/t/serve-locally-option-with-additional-scripts-and-style-sheets/6974/6

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 manage.py test fails (django.contrib.messages.middleware.MessageMiddleware)

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

Categories

Resources