DoesNotExists query in Python/Django when I build an E-commerce website - python

I am making a website for myself. When I tried making a cart, it was crashing, but everything else works perfectly.
Here is the error:
Traceback (most recent call last):
File "C:\Users\Kali\PycharmProjects\djangoProject\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Kali\PycharmProjects\djangoProject\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Kali\PycharmProjects\djangoProject\venv\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Kali\PycharmProjects\djangoProject\venv\lib\site-packages\django\views\generic\base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\Kali\PycharmProjects\djangoProject\shop\mainapp\views.py", line 47, in get
customer = Customer.objects.get(user=request.user)
File "C:\Users\Kali\PycharmProjects\djangoProject\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Kali\PycharmProjects\djangoProject\venv\lib\site-packages\django\db\models\query.py", line 429, in get
raise self.model.DoesNotExist(
mainapp.models.Customer.DoesNotExist: Customer matching query does not exist.

You are using the query Customer.objects.get(....) without a try except at line 47 of file C:\Users\Kali\PycharmProjects\djangoProject\shop\mainapp\views.py
This means that the customer you are trying to retrieve does not exist in the database. Countercheck the parameters you are using to get the Customer.
Your code should be:
try:
customer = Customer.objects.get(....)
except Customer.DoesNotExist as e:
...

Related

How to access the Data of an model using particular id in django rest framework

I want to access the data of an model which is named as IssueReport by using it's ID in django rest framework so I used the post method for passing the ID manually to get the IssueReport data by that ID but it gives me the Error as,
Method Not Allowed: /app/auth/getissuereport/
[10/Aug/2022 23:26:21] "GET /app/auth/getissuereport/ HTTP/1.1" 405 7036
Internal Server Error: /app/auth/getissuereport/
Traceback (most recent call last):
File "D:\MMRDA\.venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "D:\MMRDA\.venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\MMRDA\.venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "D:\MMRDA\.venv\lib\site-packages\django\views\generic\base.py", line 84, in view
return self.dispatch(request, *args, **kwargs)
File "D:\MMRDA\.venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "D:\MMRDA\.venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "D:\MMRDA\.venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "D:\MMRDA\.venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "D:\MMRDA\mmrda\apis\views.py", line 195, in post
report=IssueReport.objects.get(pk=id)
File "D:\MMRDA\.venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "D:\MMRDA\.venv\lib\site-packages\django\db\models\query.py", line 496, in get
raise self.model.DoesNotExist(
DataSet.models.IssueReport.DoesNotExist: IssueReport matching query does not exist.
This is my views.py,
class GetIssueReportById(generics.GenericAPIView):
serializer_class=GetIssueReportSerializer
def post(self,request,*args,**kwargs):
serializer=self.get_serializer(data=request.data)
if serializer.is_valid(raise_exception=True):
id=serializer.data.get('id')
report=IssueReport.objects.get(pk=id)
data=IssueReportSerializer(report,context=self.get_serializer_context()).data
return Response({
'message':"Data Fetched successfully",
'status':"success",
'data':data,
},status=status.HTTP_200_OK)
else:
return Response({
'message':"Data not fetched",
'status':status.HTTP_400_BAD_REQUEST
})
this is my serializers.py,
class IssueReportSerializer(serializers.ModelSerializer):
class Meta:
model=IssueReport
fields='__all__'
class GetIssueReportSerializer(serializers.ModelSerializer):
class Meta:
model=IssueReport
fields=('id',)
So can someone help me out to access the data by using ID only or any other way,
Thank you in advance.
You obviously sent GET request to your server, but in class GetIssueReportById(generics.GenericAPIView): you declared only post method.
Either define def get(): method in class GetIssueReportById, or send POST request to the server (may be with some REST client from browser plugin or VS Code plugin). If you get id argument with id=serializer.data.get('id'), id field must be in POST body.

can not save pages any more in wagtail

when clicking publish or save draft I got this error
wagtail.core.models.Page.DoesNotExist: Page matching query does not exist.
this happens only with old pages, the newly created pages are being able to be created and saved without any errors
NB: the new pages do not have children pages yet
trying to find out what is causing this error despite I did not override the save method
any suggestion or hint will be very helpful thank you
here the traceback:
wagtail.core.models.DoesNotExist
wagtail.core.models.Page.DoesNotExist: Page matching query does not exist.
Traceback (most recent call last)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/contrib/staticfiles/handlers.py", line 76, in __call__
return self.application(environ, start_response)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/core/handlers/wsgi.py", line 133, in __call__
response = self.get_response(request)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/core/handlers/base.py", line 130, in get_response
response = self._middleware_chain(request)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/core/handlers/exception.py", line 49, in inner
response = response_for_exception(request, exc)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/core/handlers/exception.py", line 103, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/core/handlers/exception.py", line 138, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django_extensions/management/technical_response.py", line 37, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/six.py", line 702, in reraise
raise value.with_traceback(tb)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/admin/urls/__init__.py", line 127, in wrapper
return view_func(request, *args, **kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/admin/auth.py", line 172, in decorated_view
response = view_func(request, *args, **kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/admin/views/pages/edit.py", line 131, in dispatch
return super().dispatch(request)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/views/generic/base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/admin/views/pages/edit.py", line 218, in post
return self.form_valid(self.form)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/admin/views/pages/edit.py", line 238, in form_valid
return self.publish_action()
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/admin/views/pages/edit.py", line 286, in publish_action
revision.publish(
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/core/models.py", line 2972, in publish
page.save()
File "/home/oladhari/gounite-v2/GOunite/v2/home/models.py", line 98, in save
super().save(*args, **kwargs)
File "/usr/lib/python3.8/contextlib.py", line 75, in inner
return func(*args, **kwds)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/core/models.py", line 1028, in save
result = super().save(**kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/modelcluster/models.py", line 199, in save
getattr(self, relation).commit()
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/modelcluster/fields.py", line 202, in commit
item.delete()
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/wagtail/core/models.py", line 1094, in delete
return Page.objects.get(id=self.id).delete(*args, **kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/oladhari/.virtualenvs/gounite/lib/python3.8/site-packages/django/db/models/query.py", line 429, in get
raise self.model.DoesNotExist(
wagtail.core.models.Page.DoesNotExist: Page matching query does not exist.
The part that is throwing errors looks like code in your models that relates one page to other pages. See this line: return Page.objects.get(id=self.id).delete(*args, **kwargs)? So I think we need to know what "items" are and how the page you are saving uses them.
Please post the models for the page you are saving and the pages that are referenced as 'items'
Finally resolved it
we should not use ParentalKey with a Page model (we were using it in GOPart model which is a Page model, changing it to ForeignKey resolved the error and now we can save the HomePage
class MyModel(RoutablePageMixin, Page, Orderable):
parent_page_types: list = []
company = ParentalKey(
"home.HomePage",
on_delete=models.SET_NULL,
verbose_name=_("Company"),
null=True,
blank=False,
)
changing it to:
class MyModel(RoutablePageMixin, Page):
parent_page_types: list = []
company = models.ForeignKey(
"home.HomePage",
on_delete=models.SET_NULL,
verbose_name=_("Company"),
null=True,
blank=False,
)
a Page can not be Orderable

TypeError: __str__ returned non-string (type NoneType) With Django third party package Models

I installed the django_message package into my django app using django 2.0 and while it works on my development machine, it throws the following errors on production when I try to open the list of messages from Django Admin console.
TypeError: __str__ returned non-string (type NoneType)
[22/Mar/2019 15:08:12] ERROR [django.request:118] Internal Server Error: /admin/django_messages/message/
Traceback (most recent call last):
File "/opt/site_env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/opt/site_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/opt/site_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/options.py", line 574, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/opt/site_env/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/opt/site_env/lib/python3.6/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 223, in inner
return view(request, *args, **kwargs)
File "/opt/site_env/lib/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper
return bound_func(*args, **kwargs)
File "/opt/site_env/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/opt/site_env/lib/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1570, in changelist_view
cl = self.get_changelist_instance(request)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/options.py", line 705, in get_changelist_instance
self,
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/views/main.py", line 76, in __init__
self.queryset = self.get_queryset(request)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/views/main.py", line 320, in get_queryset
filters_use_distinct) = self.get_filters(request)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/views/main.py", line 130, in get_filters
self.model, self.model_admin, field_path=field_path
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/filters.py", line 157, in create
return list_filter_class(field, request, params, model, model_admin, field_path=field_path)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/filters.py", line 168, in __init__
self.lookup_choices = self.field_choices(field, request, model_admin)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/filters.py", line 195, in field_choices
return field.get_choices(include_blank=False)
File "/opt/site_env/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 812, in get_choices
limit_choices_to)]
File "/opt/site_env/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 811, in <listcomp>
for x in rel_model._default_manager.complex_filter(
File "/opt/site_env/lib/python3.6/site-packages/django/utils/encoding.py", line 34, in smart_text
return force_text(s, encoding, strings_only, errors)
File "/opt/site_env/lib/python3.6/site-packages/django/utils/encoding.py", line 67, in force_text
s = str(s)
TypeError: __str__ returned non-string (type NoneType)
The biggest concern is that this is not the only model that is throwing the error. I have the same from django-allauth throwing the same error with SocialAccount.
Another concern is that I don't think I should be fixing the codes from this packages but I am left with nothing else.
Also to note, that none of the models for the application itself causes these error.
Only applies to django_allauth and django_messages. However, the failure doesn't happen in my development machine.
The mysql table for django_messages is empty (without records).
Is there a config that causes these kinds of errors or somethings.
I am using django 2.0 and python 3.6 in both enviroments.
Figured out.
It turns out that all the models failing are related to the User model whose str() method points to a field that has its database column as NULLs.
Solution: I have to make that field not nullable at the database level and at django level by assigning default value at the pre_save phase.
It worked.

ValueError in py2neo graph query

I'm trying to do a simple graph query in neo4j from python. My query looks like
def do_query(self):
graph = Graph(settings.NEO4J_CONNECTION_STRING)
query = 'MATCH (n:SomeNode) WHERE n.id = ' + id + ' RETURN n'
data = graph.cypher.execute(query)
but when I run it in a Django REST handler, I get the following error
Traceback (most recent call last):
File "/Users/me/.virtualenvs/app/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/me/.virtualenvs/app/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/me/.virtualenvs/app/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/me/.virtualenvs/app/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/me/.virtualenvs/app/lib/python2.7/site-packages/rest_framework/views.py", line 466, in dispatch
response = self.handle_exception(exc)
File "/Users/me/.virtualenvs/app/lib/python2.7/site-packages/rest_framework/views.py", line 463, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/me/Development/app/search/api/views.py", line 351, in get
results = graph_queries.do_query(user_id)
File "/Users/me/Development/app/app/graph_queries.py", line 43, in do_query
data = graph.cypher.execute(query)
File "/Users/me/.virtualenvs/app/lib/python2.7/site-packages/py2neo/core.py", line 661, in cypher
metadata = self.resource.metadata
File "/Users/me/.virtualenvs/app/lib/python2.7/site-packages/py2neo/core.py", line 213, in metadata
self.get()
File "/Users/me/.virtualenvs/app/lib/python2.7/site-packages/py2neo/core.py", line 258, in get
response = self.__base.get(headers=headers, redirect_limit=redirect_limit, **kwargs)
File "/Users/me/.virtualenvs/app/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 966, in get
return self.__get_or_head("GET", if_modified_since, headers, redirect_limit, **kwargs)
File "/Users/me/.virtualenvs/app/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 943, in __get_or_head
return rq.submit(redirect_limit=redirect_limit, **kwargs)
File "/Users/me/.virtualenvs/app/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 433, in submit
http, rs = submit(self.method, uri, self.body, self.headers)
File "/Users/me/.virtualenvs/app/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 302, in submit
raise ValueError("Unsupported URI scheme " + repr(uri.scheme))
ValueError: Unsupported URI scheme None
I'm not really sure where to begin. I'm pretty confident the connection to Neo4J is good, and the handler is being invoked correctly as shown in the traceback. Is there something wrong with my query?
Turns out the problem was I was using import settings rather than from django.conf import settings, which meant I wasn't reading the correct setting.

KeyError: 'wizard_activation_wizard'

I recently upgraded from Django 1.6.11 to Django 1.8.9, and installed django-formtools==1.0 as part of the process. I am getting this error, which does not point to a single line of my code. Has anyone come across this?
Internal Server Error: /accounts/activation-process/maria-cristo-2/
Traceback (most recent call last):
File "/home/vagrant/virtualenvs/staging/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/vagrant/virtualenvs/staging/local/lib/python2.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/vagrant/virtualenvs/staging/local/lib/python2.7/site-packages/formtools/wizard/views.py", line 237, in dispatch
response = super(WizardView, self).dispatch(request, *args, **kwargs)
File "/home/vagrant/virtualenvs/staging/local/lib/python2.7/site-packages/django/views/generic/base.py", line 89, in dispatch
return handler(request, *args, **kwargs)
File "/home/vagrant/virtualenvs/staging/local/lib/python2.7/site-packages/formtools/wizard/views.py", line 300, in post
return self.render_done(form, **kwargs)
File "/home/vagrant/virtualenvs/staging/local/lib/python2.7/site-packages/formtools/wizard/views.py", line 358, in render_done
self.storage.reset()
File "/home/vagrant/virtualenvs/staging/local/lib/python2.7/site-packages/formtools/wizard/storage/base.py", line 34, in reset
wizard_files = self.data[self.step_files_key]
File "/home/vagrant/virtualenvs/staging/local/lib/python2.7/site-packages/formtools/wizard/storage/session.py", line 13, in _get_data
return self.request.session[self.prefix]
File "/home/vagrant/virtualenvs/staging/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py", line 48, in __getitem__
return self._session[key]
KeyError: 'wizard_activation_wizard'

Categories