Django error 'too many values to unpack' in objects.get(id) - python

I have url like this http://gagandeepdesk/2690/ which I am filtering with urls.py file and sending 2690 as one of the parameters to the function.
my urls.py filter looks like this,
url(r'^(\d+)/', 'scango.scanner.views.download'),
And that function looks like this,
def download(request, MediaId):
media = Media.objects.get(id=int(MediaId))
#print media
return HttpResponse(FileIterWrapper(open(media.path)))
I am getting error 'too many values to unpack' on line
media = Media.objects.get(id=int(MediaId))
I also tried running this function standalone without request object and it is running perfectly fine. So, I am confused what I have done wrong here.
Environment:
Request Method: GET
Request URL: http://gagandeepdesk/2690/
Django Version: 1.3
Python Version: 2.7.1
Installed Applications:
['haystack',
'scanner',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin']
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:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Users\gagasing\Desktop\ideas\scango\..\scango\scanner\views.py" in download
21. media = Media.objects.get(id=int(MediaId))
File "C:\Python27\lib\site-packages\django\db\models\manager.py" in get
132. return self.get_query_set().get(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in get
343. clone = self.filter(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in filter
552. return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
570. clone.query.add_q(Q(*args, **kwargs))
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_q
1170. can_reuse=used_aliases, force_having=force_having)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_filter
1013. arg, value = filter_expr
Exception Type: ValueError at /2690/
Exception Value: too many values to unpack

(r'^(?P<MediaId>\d+)/$', 'scango.scanner.views.download')
Maybe try this instead?
This is an example of named groups.
In Python regular expressions, the syntax for named regular expression
groups is (?Ppattern), where name is the name of the group and
pattern is some pattern to match.

Related

Django Allauth TypeError at /accounts/confirm-email/

I'm using django-allauth for my django authentication and
while confirming the email i get
TypeError at /accounts/confirmemail/MQ:1mk57U:HtWDA8B5NClWhK2L6nDxJgwlNRGItW_4FyhDqcbcfow/ and it's complaining about argument of type 'bool' is not iterable
as I searched answers were in the cause of using django-rest-allauth and here I'm not using any rest api and facing this issue.
some configs on my settings.py file
# all auth config
AUTHENTICATION_BACKENDS = [
# Needed to login by username in Django admin, regardless of `allauth`
"django.contrib.auth.backends.ModelBackend",
# `allauth` specific authentication methods, such as login by e-mail
"allauth.account.auth_backends.AuthenticationBackend",
]
SITE_ID = 1
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_AUTHENTICATION_METHOD = "username_email"
ACCOUNT_LOGIN_ON_PASSWORD_RESET = True
Update: The full error tracback looks
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/confirm-email/MQ:1mk57U:HtWDA8B5NClWhK2L6nDxJgwlNRGItW_4FyhDqcbcfow/
Django Version: 3.2.9
Python Version: 3.9.7
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.facebook',
'allauth.socialaccount.providers.twitter',
'allauth.socialaccount.providers.telegram',
'allauth.socialaccount.providers.instagram',
'django_extensions',
'avatar',
'django_cleanup.apps.CleanupConfig',
'user']
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 (most recent call last):
File "/home/ali/.local/lib/python3.9/site-packages/django/shortcuts.py", line 130, in resolve_url
return reverse(to, args=args, kwargs=kwargs)
File "/home/ali/.local/lib/python3.9/site-packages/django/urls/base.py", line 86, in reverse
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
File "/home/ali/.local/lib/python3.9/site-packages/django/urls/resolvers.py", line 694, in _reverse_with_prefix
raise NoReverseMatch(msg)
During handling of the above exception (Reverse for 'True' not found. 'True' is not a valid view function or pattern name.), another exception occurred:
File "/home/ali/.local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/ali/.local/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ali/.local/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/home/ali/.local/lib/python3.9/site-packages/django/views/generic/base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "/home/ali/.local/lib/python3.9/site-packages/allauth/account/views.py", line 295, in get
return self.post(*args, **kwargs)
File "/home/ali/.local/lib/python3.9/site-packages/allauth/account/views.py", line 334, in post
return redirect(redirect_url)
File "/home/ali/.local/lib/python3.9/site-packages/django/shortcuts.py", line 41, in redirect
return redirect_class(resolve_url(to, *args, **kwargs))
File "/home/ali/.local/lib/python3.9/site-packages/django/shortcuts.py", line 136, in resolve_url
if '/' not in to and '.' not in to:
Exception Type: TypeError at /accounts/confirm-email/MQ:1mk57U:HtWDA8B5NClWhK2L6nDxJgwlNRGItW_4FyhDqcbcfow/
Exception Value: argument of type 'bool' is not iterable
The proplem come from after email confirmation done the reverse url is not valid so you need to go to your setting and check setting for that you must give it valid url to be redircted after confirmation
So I hope to revise the urls

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.

save multiple object using .add

in my previous question how-to-save-many-to-many-relationship-in-django helped me about how to save many to many but i have some issue to save. Here we go..
I have the forloop template like
{% for items in store %}
<input type="checkbox" value="{{ items.id|safe }}" name="store[]">
{% endfor %
And the mission here is i am going to save many to many relation.
so
if request.method == 'POST':
...
new_track.save()
some_var = request.POST.getlist('store[]')
some_var giving me this [u'2', u'4', u'3']
new_store = Store.objects.filter(id__in=some_var)
pprint.pprint(new_store)
new_track.store.add(new_store)
new_store giving me this [<Store: Store object>, <Store: Store object>, <Store: Store object>]. Previously i tried with objects.get() that was working but saving multiple i come to use filter but don't know how to save multiple object. Now i have the error is
int() argument must be a string or a number, not 'QuerySet'
Updated:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/dashboard/track_info/10/
Django Version: 1.7
Python Version: 2.7.6
Installed Applications:
('django.contrib.sites',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dsp',
'frontend',
'ckeditor',
'social.apps.django_app.default')
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/ri/studio/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "/home/ri/studio/Audiotube/dsp/decorators.py" in inner
46. return view(request, *args, **kwargs)
File "/home/ri/studio/Audiotube/dsp/views.py" in track_infodetail
338. new_track.store.add(new_store)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in add
917. self._add_items(self.source_field_name, self.target_field_name, *objs)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in _add_items
1010. '%s__in' % target_field_name: new_ids,
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/query.py" in filter
691. return self._filter_or_exclude(False, *args, **kwargs)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
709. clone.query.add_q(Q(*args, **kwargs))
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
1287. clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in _add_q
1314. current_negated=current_negated, connector=connector)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_filter
1181. lookups, value)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in get_lookup_constraint
1550. root_constraint.add(lookup_class(Col(alias, targets[0], sources[0]), value), AND)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/lookups.py" in __init__
82. self.rhs = self.get_prep_lookup()
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/lookups.py" in get_prep_lookup
85. return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
648. return [self.get_prep_value(v) for v in value]
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
915. return int(value)
Exception Type: TypeError at /dashboard/track_info/10/
Exception Value: int() argument must be a string or a number, not 'QuerySet'
add takes multiple objects as arguments. This should work
new_track.store.add(*new_store)
Django Documentation on Many-to-Many
new_store is a queryset, not a single object. Using the get() method will only return a single model object.
Instead, you must interact with each object in the queryset if you wish to use the same process that was working for get(). As such, you can use for each_store in new_store: new_track.store.add(each_store)
(Edited to get rid of conjecture; the above will work but there should be more performance-friendly ways to do this if that is a concern)
I suspect that the error is related to Django attempting to pass the pk of a single object for the add function, preparing the value as an integer before doing some database operation. Expecting an integer to find the field but instead getting passed the queryset object type spits out the error you see.

Django admin/ ViewDoesNotExist

I checked the other questions on here like this one, but couldn't get an accurate answer that would solve my problem. When i run the dev server with python manage.py runserver I get the following `
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.6.4
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',
'firstapp')
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/Python/2.7/site-packages/Django-1.6.4-py2.7.egg/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/Django-1.6.4-py2.7.egg/django/contrib/admin/sites.py" in wrapper
215. return self.admin_view(view, cacheable)(*args, **kwargs)
File "/Library/Python/2.7/site-packages/Django-1.6.4-py2.7.egg/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/Django-1.6.4-py2.7.egg/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/Django-1.6.4-py2.7.egg/django/contrib/admin/sites.py" in inner
194. current_app=self.name):
File "/Library/Python/2.7/site-packages/Django-1.6.4-py2.7.egg/django/core/urlresolvers.py" in reverse
503. app_list = resolver.app_dict[ns]
File "/Library/Python/2.7/site-packages/Django-1.6.4-py2.7.egg/django/core/urlresolvers.py" in app_dict
329. self._populate()
File "/Library/Python/2.7/site-packages/Django-1.6.4-py2.7.egg/django/core/urlresolvers.py" in _populate
303. lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))
File "/Library/Python/2.7/site-packages/Django-1.6.4-py2.7.egg/django/core/urlresolvers.py" in callback
230. self._callback = get_callable(self._callback_str)
File "/Library/Python/2.7/site-packages/Django-1.6.4-py2.7.egg/django/utils/functional.py" in wrapper
32. result = func(*args)
File "/Library/Python/2.7/site-packages/Django-1.6.4-py2.7.egg/django/core/urlresolvers.py" in get_callable
118. (lookup_view, mod_name))
Exception Type: ViewDoesNotExist at /admin/
Exception Value: Could not import Chinatown.views. View does not exist in module Chinatown.
`
But when i check my other project with which i follow the official django tut I dont have the mentioned views file in the project directory but I am able to see the admin page. Any suggestions why is the error being raised?
i think it will be more useful if just copy / paste error log . use:
./manage.py runserver --traceback
you need to add function in chinatown/views.py say "food"
and a url pattern in project's urls.py:
from django.conf.urls import url
urlpatterns = [
url(r'^food/$', 'chinatown.views.food'),
]
https://docs.djangoproject.com/en/dev/topics/http/urls/

__init__() got an unexpected keyword argument 'default' in json/__init__.py in dumps

I have a django server running, and, out of nowhere, I got this error. I really have no idea what changes led to this.
I'm trying to solve this for hours, but I can't find why this is happening.
Please give me a few ideas.
Ask me if you want a specific part of my code. Although the Python Path is not mentioning any of my files..
Full traceback :
Environment:
Request Method: GET
Request URL: http://whimbee-django.alwaysdata.net/api/v2/eventchat/?format=json
Django Version: 1.4.1
Python Version: 2.6.6
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'tastypie',
'core',
'iospush')
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',
'core.middleware.XsSharing')
Traceback:
File "/usr/local/alwaysdata/python/django/1.4.1/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/alwaysdata/python/django/1.4.1/django/views/decorators/csrf.py" in wrapped_view
77. return view_func(*args, **kwargs)
File "/home/whimbee-django/modules/django_tastypie-0.9.15-py2.6.egg/tastypie/resources.py" in wrapper
264. return self._handle_500(request, e)
File "/home/whimbee-django/modules/django_tastypie-0.9.15-py2.6.egg/tastypie/resources.py" in _handle_500
286. return self.error_response(request, data, response_class=response_class)
File "/home/whimbee-django/modules/django_tastypie-0.9.15-py2.6.egg/tastypie/resources.py" in error_response
1243. serialized = self.serialize(request, errors, desired_format)
File "/home/whimbee-django/modules/django_tastypie-0.9.15-py2.6.egg/tastypie/resources.py" in serialize
397. return self._meta.serializer.serialize(data, format, options)
File "/home/whimbee-django/modules/django_tastypie-0.9.15-py2.6.egg/tastypie/serializers.py" in serialize
187. serialized = getattr(self, "to_%s" % desired_format)(bundle, options)
File "/home/whimbee-django/modules/django_tastypie-0.9.15-py2.6.egg/tastypie/serializers.py" in to_json
356. return simplejson.dumps(data, cls=json.DjangoJSONEncoder, sort_keys=True, ensure_ascii=False)
File "/usr/languages/python/2.6/lib/python2.6/json/__init__.py" in dumps
237. **kw).encode(obj)
Exception Type: TypeError at /api/v2/eventchat/
Exception Value: __init__() got an unexpected keyword argument 'default'
From a quick look at the json code, it seems to be calling cls() with a keyword default. By default, cls=JSONEncoder, but in this case, it is json.DjangoJSONEncoder (defined in django_tastypie), which seems not to comply with that implicit requirement, of taking a default keyword.
From the docs:
To use a custom JSONEncoder subclass (e.g. one that overrides the
.default() method to serialize additional types), specify it with
the cls kwarg; otherwise JSONEncoder is used.
Perhaps you should try upgrading to the latest version of tastypie, and/or report a bug.

Categories

Resources