Traceback (most recent call last):
File "/home/ahmed/SavannahX/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/ahmed/SavannahX/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ahmed/SavannahX/venv/lib/python3.9/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/ahmed/SavannahX/app/views.py", line 1352, in yearly_subscription_plans
duration = Duration.objects.get(name="yearly")
File "/home/ahmed/SavannahX/venv/lib/python3.9/site-packages/```
django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/ahmed/SavannahX/venv/lib/python3.9/site-packages/django/db/models/query.py", line 496, in get
raise self.model.DoesNotExist(
Exception Type: DoesNotExist at /yearly/subscriptions/
Exception Value: Duration matching query does not exist.
You are using the .get() function but it has not found a matching object. This is the danger of using get, it raises an exception if the object isn't found. The .filter() function by contrast will just return an empty queryset. You can either:
Use the get_object_or_404 function that returns a 404 HTTP response if the object is not found.
from django.shortcuts import get_object_or_404
duration = get_object_or_404(Duration, pk=pk)
or
Wrap the get() in a try/except wrapper:
try:
Duration.objects.get(name="yearly")
except:
# do something else, probably return 404...
duration = Duration.objects.get(name="yearly")
above code is giving the error as get method raises the "DoesNotExist" if the object is not found and can be handled as below ways
First method
Duration.objects.filter(name="yearly").first()
Second method
from django.shortcuts import get_object_or_404
duration = get_object_or_404(Duration, pk=pk)
Third method
try:
Duration.objects.get(name="yearly")
except:
#please handle the exception here
Related
"Field 'id' expected a number but got 'results'."
I'm getting this error while trying to use search filter over a list of books. It was working fine until I changed the Class-based view to Function based view.
This was the Class based view earlier using the default DetailView class:
class BookDetailView(LoginRequiredMixin,DetailView):
model = models.Book
template_name='book_detail.html'
login_url='login'
This is the new Function based detail view I changed to:
#login_required
def book_detail(request,book_id):
model =models.Book
book=model.objects.get(id=book_id)
template ='book_detail.html'
owner =CustomUser.objects.get(username=book.owner)
return render(request,template,{'book':book,'owner':owner})
Independently when I'm trying to go to detail view, its working fine. But when I try to search using the 'book_search' view, its throwing this error. Previously search functionality was also working fine.
#login_required
def book_search(request):
template ='book_list.html'
model =models.Book
query =request.GET.get('q')
results =model.objects.exclude(owner =request.user).order_by('-available','-id')
if query:
results =results.filter(Q(title__icontains =query))
paginator = Paginator(results, 9)
page =request.GET.get('page')
try:
books =paginator.page(page)
except PageNotAnInteger:
books =paginator.page(1)
except EmptyPage:
books= paginator.page(paginator.num_pages)
return render(request,template,{'books':books,'query':query,'page':page})
It has something to do with the result set that the search view is returning a list of books while detail view only require just one id.
Edit: Error stack:-
ValueError: invalid literal for int() with base 10: 'results'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Dell\.virtualenvs\read_bus-VVRhbVr5\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Dell\.virtualenvs\read_bus-VVRhbVr5\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Dell\.virtualenvs\read_bus-VVRhbVr5\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Dell\.virtualenvs\read_bus-VVRhbVr5\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\Dell\Documents\read_bus\books\views.py", line 216, in book_detail
book=results.get(id=book_id)
File "C:\Users\Dell\.virtualenvs\read_bus-VVRhbVr5\lib\site-packages\django\db\models\query.py", line 404, in get
clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
File "C:\Users\Dell\.virtualenvs\read_bus-VVRhbVr5\lib\site-packages\django\db\models\query.py", line 904, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\Dell\.virtualenvs\read_bus-VVRhbVr5\lib\site-packages\django\db\models\query.py", line 923, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\Dell\.virtualenvs\read_bus-VVRhbVr5\lib\site-packages\django\db\models\sql\query.py", line 1337, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\Dell\.virtualenvs\read_bus-VVRhbVr5\lib\site-packages\django\db\models\sql\query.py", line 1365, in _add_q
split_subq=split_subq, simple_col=simple_col,
File "C:\Users\Dell\.virtualenvs\read_bus-VVRhbVr5\lib\site-packages\django\db\models\sql\query.py", line 1298, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Users\Dell\.virtualenvs\read_bus-VVRhbVr5\lib\site-packages\django\db\models\sql\query.py", line 1155, in build_lookup
lookup = lookup_class(lhs, rhs)
File "C:\Users\Dell\.virtualenvs\read_bus-VVRhbVr5\lib\site-packages\django\db\models\lookups.py", line 22, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\Dell\.virtualenvs\read_bus-VVRhbVr5\lib\site-packages\django\db\models\lookups.py", line 72, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "C:\Users\Dell\.virtualenvs\read_bus-VVRhbVr5\lib\site-packages\django\db\models\fields\__init__.py", line 1772, in get_prep_value
) from e
For this detail_view:
URLs should look like:
urlpatterns =[
path('book/<int:pk>',views.book_detail,name ='book_detail'),
path('results/',views.book_search,name ='search'),
]
While the views function should be this:
#login_required
def book_detail(request,book_id):
model =models.Book
book=model.objects.get(id=book_id)
template ='book_detail.html'
owner =CustomUser.objects.get(username=book.owner)
return render(request,template,{'book':book,'owner':owner})
Passing value as http://127.0.0.1:8000/api/book/1
I seem to have found the solution.
It was actually the url pattern for the 'book_detail' that was causing the problem. I had created it without using path converter
urls.py:
...
urlpatterns =[
path('<book_id>/',views.book_detail,name ='book_detail'),
path('results/',views.book_search,name ='search'),
]
I thought just using <book_id> would suffice, and since it is higher in the hierarchy
of urlpatterns, it was matched for 'results' too.
Changing the path to <int:book_id> worked. Now I've realised the significance of the little path converter and won't ever forget using this.
I encounter this error for my django project. my app is called "scoresubmission"
basially i have a feature in the website to allow user download report.
So in my views.py file i have report function and import report.py file, where it shows how report is built
It shows the error happens in this line of code:
submission=Submission.objects.get(month=month,year=reportyear,program=program)
Views.py
def report(request):
from scoresubmission.report import reportA, reportB, reportC
reportType = request.POST["reportType"]
reportYear = int(request.POST["reportYear"])
if reportType == 'a':
report_content = reportA(reportYear)
response = HttpResponse(report_content, content_type="text/csv")
response['Content-Disposition'] = 'inline; filename=5SAuditYearlySummaryReport_%d.xlsx' %reportYear
report.py where it has the relevant code
for facility in facilities:
worksheet.write(row,col,facility.name,facility_format)
for i in range(12): # 12 months
month=i+1
programs=Program.objects.filter(facility_id=facility.id)
avg_totalscore=0
count=1
for program in programs:
print(program)
try:
submission=Submission.objects.get(month=month,year=reportyear,program=program)
print(submission)
avg_score=Result.objects.filter(submission=submission).aggregate(Avg('NewScore'))
#print avg_score.get('NewScore__avg')
avg_totalscore=(avg_totalscore + avg_score.get('NewScore__avg'))/count
count=count+1
except submission.DoesNotExist:
pass
#print avg_totalscore
if avg_totalscore!=0:
worksheet.write(row,i+3,avg_totalscore,red_format)
else:
worksheet.write(row,i+3,'-',red_format)
Traceback (most recent call last):
File "C:\Users\CHLOZHAO\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\CHLOZHAO\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\CHLOZHAO\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\CHLOZHAO\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\D Drive\5S Audit Website\my5saudit\scoresubmission\views.py", line 185, in report
report_content = reportA(reportYear)
File "C:\D Drive\5S Audit Website\my5saudit\scoresubmission\report.py", line 79, in reportA
except submission.DoesNotExist:
UnboundLocalError: local variable 'submission' referenced before assignment
In your except you need to refer to the class Submission, not the object, since it does not per se exists at that time:
try:
submission=Submission.objects.get(month=month,year=reportyear,program=program)
print(submission)
avg_score=Result.objects.filter(submission=submission).aggregate(Avg('NewScore'))
#print avg_score.get('NewScore__avg')
avg_totalscore=(avg_totalscore + avg_score.get('NewScore__avg'))/count
count=count+1
except Submission.DoesNotExist: # reference to the class, not the object
pass
If Sibmission.objects.get(..) fails, then the submission variable is never assigned, and hence submission.DoesNotExist makes no sense.
You acutally should never use the object, and always use the model class itself to refer to the DoesNotExist exception class.
I have a class in which I am checking user permissions, and depending on them, I return a list of results from models. Here is what I have:
from AnnualLeave.models import Leaves, RejectedLeaves, Employee
class GetLeaves:
def get_results(request):
try:
if request.user.groups.filter(name__in=['SuperAdmin']).exists():
return Leaves.objects.all()
elif request.user.groups.filter(name__in=['Admin']).exists():
return Leaves.objects.filter(employee_id=Employee.objects.get(manager_id=request.user.pk))
else:
messages.error(request, "You are not allowed on this page")
return render(request, 'users/home.html')
except (Leaves.DoesNotExist, Employee.DoesNotExist):
return []
def process_results(request):
leave_request = []
for leave in GetLeaves.get_results(request):
content = {'emp_id': leave.employee_id,
'emp_name': Employee.objects.get(user_id=leave.employee_id).user.get_full_name(),
'start_date': leave.start_date,
'end_date': leave.end_date,
'reason': leave.get_reason_display(),
'description': leave.description,
}
....
leave_request.append(content)
return leave_request
And then I'm calling process_results function in TemplateView like this:
class ProcessLeaveRequest(TemplateView):
template_name = 'LMSAdmin/process_leave_request.html'
def get(self, request, *args, **kwargs):
return render(request, self.template_name, {'leave_requests': GetLeaves.process_results(request)})
Here is a traceback of error:
Internal Server Error: /lms_admin/upcomingleaves/
Traceback (most recent call last):
File "C:\Program Files (x86)\Python37-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "D:\Projects\LMS\LMSAdmin\views.py", line 77, in get
for leave in GetLeaves.process_results(request):
File "D:\Projects\LMS\LMSAdmin\views.py", line 39, in process_results
content = {'emp_id': leave.employee_id,
AttributeError: 'bytes' object has no attribute 'employee_id'
From what I can see, it is reading the entire webpage with the request parameter in functions. Like the get_results function is not returning a list, but the entire webpage. I do not understand why this is happening because it was working fine before without any changes.
You should not return a HttpRequest from get_results. Instead raise a PermissionDenied error.
else:
from django.core.exceptions import PermissionDenied
messages.error(request, "You are not allowed on this page")
raise PermissionDenied
If you want to redirect to a different page, handle that in the view. You can capture the PermissionDenied and return the redirect.
I have a query in mind, currently it works as expected in shell with the Django ORM:
>>> Place.objects.all()[0].images.filter(order=0)[0].filename
'y5IUMPyv.jpg'
But I don't know how to implement it with DRF's SerializerMethodField. This is what I'm using in the meantime until I can figure it out:
class CardSerializer(serializers.Serializer):
image = serializers.SerializerMethodField()
#staticmethod
def get_image(obj):
for d in obj.images.all():
if d.order == 0:
return d.filename
This is the "ideal" method that doesn't work, and I don't know why:
#staticmethod
def get_image(obj):
return obj.images.filter(order=0)[0].filename
Traceback (most recent call last):
File "/home/admin/env/lib/python3.4/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/admin/env/lib/python3.4/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/admin/env/lib/python3.4/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/admin/env/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/admin/env/lib/python3.4/site-packages/rest_framework/views.py", line 466, in dispatch
response = self.handle_exception(exc)
File "/home/admin/env/lib/python3.4/site-packages/rest_framework/views.py", line 463, in dispatch
response = handler(request, *args, **kwargs)
File "/home/admin/env/lib/python3.4/site-packages/rest_framework/generics.py", line 201, in get
return self.list(request, *args, **kwargs)
File "/home/admin/env/lib/python3.4/site-packages/rest_framework/mixins.py", line 45, in list
return self.get_paginated_response(serializer.data)
File "/home/admin/env/lib/python3.4/site-packages/serpy/serializer.py", line 134, in data
self._data = self.to_value(self.instance)
File "/home/admin/env/lib/python3.4/site-packages/serpy/serializer.py", line 123, in to_value
return [serialize(o, fields) for o in instance]
File "/home/admin/env/lib/python3.4/site-packages/serpy/serializer.py", line 123, in <listcomp>
return [serialize(o, fields) for o in instance]
File "/home/admin/env/lib/python3.4/site-packages/serpy/serializer.py", line 107, in _serialize
result = getter(self, instance)
File "/home/admin/src/places/serializers.py", line 72, in get_image
return obj.images.filter(order=0)[0].filename
File "/home/admin/env/lib/python3.4/site-packages/django/db/models/query.py", line 297, in __getitem__
return list(qs)[0]
IndexError: list index out of range
As the iron maiden #anderson-lima has pointed out, this is a problem with your data rather than your code. You do not have an image with order = 0 and your first method handles that situation correct if not optimally.
#staticmethod
def get_image(obj):
for d in obj.images.all():
if d.order == 0:
return d.filename
# returns None here if an object with order = 0
# does not exist in the database.
However in your second approach you are taking a slice but fetching an object that does not exist. Hence the execption, and which in turn tells us that what you need is a just a try except block.
#staticmethod
def get_image(obj):
try:
return obj.images.filter(order=0)[0].filename
except IndexError:
return None
I try to implement a view based on BaseDeleteView for a website that acts as a frontend to an REST backend. Both sides communicate over HTTP requests. What I want to achieve is that I send a GET request to an activation URI (send per email after registration). Inside this view I first send a HTTP request to a backend, and then delete the activation object from the database of the frontend. I don't want to have a confirmation page, so DeleteView is not possible.
class ActivationView(BaseDeleteView):
success_url = "/activation/success/"
def get_object(self, queryset=None):
uuid = self.kwargs['uuid']
try:
obj = AccountRegistration.objects.get(uuid=uuid)
except ObjectDoesNotExist:
raise Http404('Registration not found.')
return obj
def delete(self, request, *args, **kwargs):
obj = self.get_obj()
if obj.expire_date < datetime.now():
obj.delete()
raise Http404('Registration expired.')
# send a http request to the backend
t = Transaction('/activate/%s/' % obj.account_name)
t.emit()
# delete the object
obj.delete()
# and redirect the request
return HttpResponseRedirect(self.get_success_url())
My urls.py looks like that:
url(r'^activate/(?P<uuid>\w+)/$',
ActivationView.as_view(), name="account-activate"),
But I get the following error:
Traceback (most recent call last):
File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 68, in __call__
return self.application(environ, start_response)
File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 272, in __call__
response = self.get_response(request)
File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/core/handlers/base.py", line 169, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/core/handlers/base.py", line 203, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/views/generic/base.py", line 47, in view
return self.dispatch(request, *args, **kwargs)
File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in dispatch
return handler(request, *args, **kwargs)
File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/views/generic/detail.py", line 100, in get
return self.render_to_response(context)
AttributeError: 'ActivationView' object has no attribute 'render_to_response'
In my eyes it shouldn't even call render_to_response. Any ideas?
If you want to leave out the confirmation page, just call your DeleteView directly with POST. This is most desirable as the deletion of an object should be protected by csrf.
You've inherited from BaseDeleteView, which as the documentation states, doesn't include the TemplateResponseMixin - ie all the bits that are to do with rendering a response.
Inherit from DeleteView instead.