Error in function "int() argument must be a string" - python

I have issues with the following code in my Django project.
Anyone one have a clue what's wrong?
Exception Type: TypeError at /tasks/2/3/ Exception Value: int() argument must be a string, a bytes-like object or a number, not 'DeferredAttribute'
(Django 2.1)
def step_detail(request, task_pk, step_pk):
user = request.user
steps = Step.objects.filter(task_id=task_pk)
step = get_object_or_404(Step, task_id=task_pk, pk=step_pk)
next = ""
if int(Step.step_number) < len(steps):
next = "/%s/%s/" % (task_pk, int(step_pk) + 1)
user.userprofile.current_step.update(step_pk)
else:
next = "/task_done/"
user.userprofile.current_step.update(step_pk)
user.userprofile.daily_task_done_time(datetime.datetime.now())
if request.POST:
if "nextstep" in request.POST:
return redirect(request.POST.get('next'))
return render(request, 'dailytask/step_detail.html', {'step': step})
Environment:
Request Method: GET Request URL: http://127.0.0.1:8000/tasks/2/3/
Django Version: 2.1.1 Python Version: 3.7.1 Installed Applications:
['django.contrib.admin', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.staticfiles',
'dailytask', 'account'] 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
"/Users/iamsuccessful/eb-virt/lib/python3.7/site-packages/django/core/handlers/exception.py"
in inner
34. response = get_response(request)
File
"/Users/iamsuccessful/eb-virt/lib/python3.7/site-packages/django/core/handlers/base.py"
in _get_response
126. response = self.process_exception_by_middleware(e, request)
File
"/Users/iamsuccessful/eb-virt/lib/python3.7/site-packages/django/core/handlers/base.py"
in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File
"/Users/iamsuccessful/eb-virt/lib/python3.7/site-packages/django/contrib/auth/decorators.py"
in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "/Users/iamsuccessful/ebdjango/dailytask/views.py" in step_detail
60. if int(Step.step_number) < int(len(steps)):
Exception Type: TypeError at /tasks/2/3/ Exception Value: int()
argument must be a string, a bytes-like object or a number, not
'DeferredAttribute'

Related

TypeError at /login Strings must be encoded before hashing

I am facing an error from this Django project. I have tried all the advice i can come across still its not working properly.
Below is the error from the web.
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/login
Django Version: 2.2
Python Version: 3.9.7
Installed Applications:
['main',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_filters']
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\INTROVERTED\Downloads\Compressed\FacialRecognitionForPolice-master\venv1\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\INTROVERTED\Downloads\Compressed\FacialRecognitionForPolice-master\venv1\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\INTROVERTED\Downloads\Compressed\FacialRecognitionForPolice-master\venv1\lib\site-packages\django\core\handlers\base.py" in _get_response
113.`response = wrapped_callback(request, *callback_args, **callback_kwargs)`
File "C:\Users\INTROVERTED\Downloads\Compressed\FacialRecognitionForPolice-master\FacialRecognitionForPolice-master\main\views.py" in login
171.`hashed = bcrypt.hashpw(request.POST['login_password'], bcrypt.gensalt())`
File "C:\Users\INTROVERTED\Downloads\Compressed\FacialRecognitionForPolice-master\venv1\lib\site-packages\bcrypt\__init__.py" in hashpw
79.`raise TypeError("Strings must be encoded before hashing")`
Exception Type: TypeError at /login
Exception Value: Strings must be encoded before hashing
As error message clearly says, you have to encode the password first.
In main/views.py on line 171 you have
hashed = bcrypt.hashpw(request.POST['login_password'], bcrypt.gensalt())
Your request.POST data is a string, so you should encode it to bytes:
hashed = bcrypt.hashpw(request.POST['login_password'].encode(), bcrypt.gensalt())
You can select other encoding (not default utf8) with .encode('ascii') (choose one and don't change it later, utf8 is probably the best choice in general).

Django - 'User' object has no attribute 'session_set'

I want to add a Sessions tabs to my Django project:
navbar.html
<li>Sessions</li>
But i keep getting the following error:
'User' object has no attribute 'session_set''User' object has no attribute 'session_set'
Exception Location: C:\Users\User\lib\site-packages\django\utils\functional.py in inner, line 214
I don't know where this error comes from, i did not find any other reference online, can anyone help me?
Here is the login view that i'm using: https://github.com/Bouke/django-two-factor-auth/blob/master/two_factor/views/core.py
Full traceback
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/account/sessions/
Django Version: 2.1.7
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main.apps.MainConfig',
'tinymce',
'captcha',
'django.contrib.sites',
'django_otp',
'django_otp.plugins.otp_static',
'django_otp.plugins.otp_totp',
'two_factor']
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',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django_otp.middleware.OTPMiddleware']
Traceback:
File "C:\Users\User\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\User\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:\Users\User\lib\site-packages\django\core\handlers\base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\lib\site-packages\django\views\generic\base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "C:\Users\User\lib\site-packages\django\utils\decorators.py" in _wrapper
45. return bound_method(*args, **kwargs)
File "C:\Users\User\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "C:\Users\User\lib\site-packages\user_sessions\views.py" in dispatch
22. **kwargs)
File "C:\Users\User\lib\site-packages\django\views\generic\base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "C:\Users\User\lib\site-packages\django\views\generic\list.py" in get
142. self.object_list = self.get_queryset()
File "C:\Users\User\lib\site-packages\user_sessions\views.py" in get_queryset
14. return self.request.user.session_set\
File "C:\Users\User\lib\site-packages\django\utils\functional.py" in inner
214. return func(self._wrapped, *args)
File "C:\Users\User\lib\site-packages\django\utils\functional.py" in inner
214. return func(self._wrapped, *args)
Exception Type: AttributeError at /account/sessions/
Exception Value: 'User' object has no attribute 'session_set'
You need to replace django.contrib.sessions in your INSTALLED_APPS with user_sessions if you want to use django-user-sessions. This will setup its models correctly.
Furthermore make sure you have also followed all other installation instructions.

Exception Type: AttributeError at /notifier/not/ Exception Value: 'AsgiRequest' object has no attribute 'loop'

when i tried to run a piece if code in my django application i have been faced with some strange error
def main(request):
async def hello(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
msg="this is awesome"
ws.send_str(msg)
# return HttpResponse("ok")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop = asyncio.get_event_loop()
loop.run_until_complete(hello(request))
loop.close()
when i run this code
i just get
Exception Value: 'AsgiRequest' object has no attribute 'loop'
here is the complete traceback of the issue
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/notifier/not/
Django Version: 2.0.7
Python Version: 3.6.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest',
'accounts',
'connect',
'drones',
'notifier',
'rest_framework',
'rest_framework.authtoken',
'corsheaders',
'channels',
'oauth2_provider',
'social_django',
'rest_framework_social_oauth2',
'drfpasswordless',
'taggit',
'taggit_serializer',
'elasticsearch']
Installed Middleware:
['corsheaders.middleware.CorsPostCsrfMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'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\madhumani\workspace\ven\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\madhumani\workspace\ven\drfc\notifier\views.py" in main
32. loop.run_until_complete(hello(request))
File "c:\users\madhumani\appdata\local\programs\python\python36-32\Lib\asyncio\base_events.py" in run_until_complete
467. return future.result()
File "C:\Users\madhumani\workspace\ven\drfc\notifier\views.py" in hello
23. await ws.prepare(request)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\aiohttp\web_ws.py" in prepare
106. protocol, writer = self._pre_start(request)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\aiohttp\web_ws.py" in _pre_start
183. self._loop = request.loop
Exception Type: AttributeError at /notifier/not/
Exception Value: 'AsgiRequest' object has no attribute 'loop'

Django TypeError on every page

I haven't touched the code on my app in weeks but i'm suddenly getting this exact same TypeError on every page, including Admin pages.
The last thing to happen was an update on a model to change a ForeignKey field, but I don't feel the two are related
Environment:
Request Method: GET
Request URL: http://10.0.0.161:8001/admin/
Django Version: 1.9.3
Python Version: 2.7.6
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'phones',
'jobs',
'profiles',
'freelancers',
'stock',
'finance']
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.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'profiles.middleware.UpdateLastActivityMiddleware']
Traceback:
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
174. response = self.process_exception_by_middleware(e, request)
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
172. response = response.render()
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/response.py" in render
160. self.content = self.rendered_content
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/response.py" in rendered_content
137. content = template.render(context, self._request)
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/backends/django.py" in render
95. return self.template.render(context)
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/base.py" in render
204. with context.bind_template(self):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py" in __enter__
17. return self.gen.next()
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/context.py" in bind_template
260. updates.update(processor(self.request))
Exception Type: TypeError at /admin/
Exception Value: 'NoneType' object is not iterable
Solved it by looking through some of my own context processors. I had one function that was returning None on a request (after changing an entry and having an empty queryset as a result).

Filter iexact error Model matching query does not exist

I'm trying to verify if a value exists in my database with:
q = model.objects.filter(field__iexact='1')
But this makes an error in Django, which returns this message:
DoesNotExist at /answer_survey/
Model matching query does not exist
This is the entire traceback:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/answer_survey/
Django Version: 1.6.1
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'survey',
'south',
'django_reset',
'chartkick',
'registration')
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 "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/filipeferminiano/Documents/django/panorama/survey/views.py" in answer_survey
265. s = Survey.objects.get(name=cod_survey)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/manager.py" in get
151. return self.get_queryset().get(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py" in get
307. self.model._meta.object_name)
Exception Type: DoesNotExist at /answer_survey/
Exception Value: Survey matching query does not exist.
Your error is here:
s = Survey.objects.get(name=cod_survey)
you have't records with name that contains in cod_survey variable...
Add try/except block to catch DoesNotExist exceptions:
try:
s = Survey.objects.get(name=cod_survey)
except Survey.DoesNotExist:
s = 'something else...'

Categories

Resources