500-error when adding a picture - django

In Django-CMS, I select the picture plugin and add a picture from my harddrive, then when I click on that picture-plugin I get the error as seen below. What is the reason for this?
ValueError at /admin/cms/page/2/edit-plugin/7/cms_page_media/2/parking_mockup.png/
invalid literal for int() with base 10: '2/edit-plugin/7/cms_page_media/2/parking_mockup.png'
Request Method: GET
Request URL: http://localhost:8000/admin/cms/page/2/edit-plugin/7/cms_page_media/2/parking_mockup.png/
Django Version: 1.3.1
Exception Type: ValueError
Exception Value:
invalid literal for int() with base 10: '2/edit-plugin/7/cms_page_media/2/parking_mockup.png'
Exception Location: C:\Python27\lib\site-packages\django\db\models\fields\__init__.py in get_prep_value, line 479
Python Executable: C:\Python27\python.exe
Python Version: 2.7.2
Python Path:
['C:\\djangoworkspace\\hulawai',
'C:\\Python27\\lib\\site-packages\\django_classy_tags-0.3.4.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\django_sekizai-0.5-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\html5lib-0.95-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\virtualenv-1.7.1.2-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\pip-1.1-py2.7.egg',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages',
'C:\\Python27\\lib\\site-packages\\PIL']
Server time: Thu, 22 Mar 2012 11:03:58 +0100
Traceback Switch to copy-and-paste view
C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response
for middleware_method in self._view_middleware:
response = middleware_method(request, callback, callback_args, callback_kwargs)
if response:
break
if response is None:
try:
response = callback(request, *callback_args, **callback_kwargs) ...
except Exception, e:
# If the view raised an exception, run it through exception
# middleware, and if the exception middleware returns a
# response, use that. Otherwise, reraise the exception.
for middleware_method in self._exception_middleware:
response = middleware_method(request, e)
UPDATE Full stacktrace available here: https://gist.github.com/2157837

I edited my settings.py
From
MEDIA_URL = 'media/'
To
MEDIA_URL = 'http://localhost:8000/media/'

Related

What did mean by AttributeError at /login_user/?

My target is to get all the products a user added to the cart, that's why I decided to fetch the ShopingCart model from the context processor. And I added it to the context processor, and it worked well. But the problem is when I try to log out, then I get an error. Where did the actual problem occur? 😢...
models.py:
class ShopingCart(models.Model):
User = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='UserShoppingCartRelatedName',on_delete=models.CASCADE)
Product = models.ForeignKey(Products, related_name='ShoppingCartRelatedName',on_delete=models.CASCADE)
context_processors:
def ShoppingCart(request):
return {"ShoppingCart":request.user.UserShoppingCartRelatedName.all()}
error:
AttributeError at /login_user/
'AnonymousUser' object has no attribute 'UserShoppingCartRelatedName'
Request Method: GET
Request URL: http://127.0.0.1:8000/login_user/
Django Version: 4.0.4
Exception Type: AttributeError
Exception Value:
'AnonymousUser' object has no attribute 'UserShoppingCartRelatedName'
Exception Location: D:\1_WebDevelopment\17_Ecomerce Website\ecomerce site\env\lib\site-packages\django\utils\functional.py, line 259, in inner
Python Executable: D:\1_WebDevelopment\17_Ecomerce Website\ecomerce site\env\Scripts\python.exe
Python Version: 3.9.5
Python Path:
['D:\\1_WebDevelopment\\17_Ecomerce Website\\ecomerce site',
'c:\\users\\dcl\\appdata\\local\\programs\\python\\python39\\python39.zip',
'c:\\users\\dcl\\appdata\\local\\programs\\python\\python39\\DLLs',
'c:\\users\\dcl\\appdata\\local\\programs\\python\\python39\\lib',
'c:\\users\\dcl\\appdata\\local\\programs\\python\\python39',
'D:\\1_WebDevelopment\\17_Ecomerce Website\\ecomerce site\\env',
'D:\\1_WebDevelopment\\17_Ecomerce Website\\ecomerce '
'site\\env\\lib\\site-packages']
Server time: Tue, 09 Aug 2022 11:48:23 +0000
Check if user is not none and authenticated.
def ShoppingCart(request):
if request.user is not None and request.user.is_authenticated():
return {"ShoppingCart":request.user.UserShoppingCartRelatedName.all()}
else:
return {}
It looks as if you are not logged in!
You must login before you can access the user atrubute.
If you are not loged in it says you are 'AnonymousUser'
'AnonymousUser' object has no attribute 'UserShoppingCartRelatedName'
Login and then try it!!

Unable to perform conditional redirect from a class based view Django

I am trying to redirect a user who has already registered to a different view. here is the code for the views.py
However when qs.exists() = true I get an error
'The view Lpage.views.homeview didn't return an HttpResponse object. It returned None instead.'
I am a beginner have read the documentation but unable to find where i am going worng.
Thanks
from django.shortcuts import render, redirect
from django.views import View
from Lpage.forms import SubscriberEntryForm
from Lpage.models import Subscriber
class homeview(View):
def get(self,request):
msg = request.session.get('msg', False)
if(msg):
del(request.session['msg'])
return render(request,'Lpage/index.html')
def post(self, request):
form = SubscriberEntryForm(request.POST or None)
if form.is_valid():
obj = form.save(commit=False)
qs = Subscriber.objects.filter(email__iexact=obj.email)
if qs.exists():
return redirect('messageview')
else:
obj.save()
request.session['msg'] = "msg"
return redirect(request.path)
def messageview(request):
return render(request,'Lpage/messages.html',{})
Here is the error message
ValueError at /
The view Lpage.views.homeview didn't return an HttpResponse object. It returned None instead.
Request Method: POST
Request URL: http://localhost:8000/
Django Version: 3.2.7
Exception Type: ValueError
Exception Value:
The view Lpage.views.homeview didn't return an HttpResponse object. It returned None instead.
Exception Location: C:\Users\Ganesamurthi\anaconda3\lib\site-packages\django\core\handlers\base.py, line 309, in check_response
Python Executable: C:\Users\Ganesamurthi\anaconda3\python.exe
Python Version: 3.8.5
Python Path:
['D:\dreamdoors\dd',
'C:\Users\Ganesamurthi\anaconda3\python38.zip',
'C:\Users\Ganesamurthi\anaconda3\DLLs',
'C:\Users\Ganesamurthi\anaconda3\lib',
'C:\Users\Ganesamurthi\anaconda3',
'C:\Users\Ganesamurthi\anaconda3\lib\site-packages',
'C:\Users\Ganesamurthi\anaconda3\lib\site-packages\win32',
'C:\Users\Ganesamurthi\anaconda3\lib\site-packages\win32\lib',
'C:\Users\Ganesamurthi\anaconda3\lib\site-packages\Pythonwin']
Server time: Wed, 29 Sep 2021 05:23:43 +0000
Traceback Switch to copy-and-paste view
C:\Users\Ganesamurthi\anaconda3\lib\site-packages\django\core\handlers\exception.py, line 47, in inner
response = get_response(request) …
â–¶ Local vars
C:\Users\Ganesamurthi\anaconda3\lib\site-packages\django\core\handlers\base.py, line 188, in _get_response
self.check_response(response, callback) …
â–¶ Local vars
C:\Users\Ganesamurthi\anaconda3\lib\site-packages\django\core\handlers\base.py, line 309, in check_response
raise ValueError( …
â–¶ Local vars
redirect expects you to pass a URL but you gave it messageview, which is a view class in fact.
So you need to give redirect to the URL of messageview.

Pillow on Heroku doesn't work

I'm developing an application with Django1.4.3 and Pillow2.0.0.
I have a form to upload image file.
After resizing and cropping posted image file,
I want to save the image file, but error occur.
In django's local test server, error doesn't occur and work well,
but in Heroku, error occur.
Would you tell me some advice?
The code receiving posted image is below.
In addition, I use S3boto and django-storage.
def edit_photo(request):
if request.user.is_authenticated():
if request.method == 'POST':
# save posted image as UserProfile.image temporarily
posted_photo = request.FILES['posted_photo']
file_content = ContentFile(posted_photo.read())
profile = request.user.get_profile()
temp_filename = "new_file_"+str(profile.id)+"_"+posted_photo.name
profile.image.save(temp_filename, file_content)
# read posted file
data = profile.image.read()
im = Image.open(StringIO.StringIO(data))
# crop posted image
cropping_box = (10, 10, 300, 300)
photo = photo.crop(cropping_box)
photo_comp = photo.resize((230, 230), Image.ANTIALIAS)
# save the image
thum = StringIO.StringIO()
photo_comp.save(thum, "png")
profile.image.save("saved_image_"+str(profile.id)+".png",ContentFile(thum.getvalue()))
# delete temporary image
default_storage.delete("faces/"+temp_filename)
return redirect('../')
And Error message is like this.
TypeError at /manage/edit_photo
function takes at most 4 arguments (6 given)
Request Method: POST
Request URL: http://hogehoge.herokuapp.com/manage/edit_photo
Django Version: 1.4.3
Exception Type: TypeError
Exception Value:
function takes at most 4 arguments (6 given)
Exception Location: /app/.heroku/python/lib/python2.7/site-packages/PIL/Image.py in _getencoder, line 395
Python Executable: /app/.heroku/python/bin/python
Python Version: 2.7.4
Python Path:
['/app',
'/app/.heroku/python/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg',
'/app/.heroku/python/lib/python2.7/site-packages/newrelic-1.11.0.55/newrelic/bootstrap',
'/app',
'/app/.heroku/python/lib/python27.zip',
'/app/.heroku/python/lib/python2.7',
'/app/.heroku/python/lib/python2.7/plat-linux2',
'/app/.heroku/python/lib/python2.7/lib-tk',
'/app/.heroku/python/lib/python2.7/lib-old',
'/app/.heroku/python/lib/python2.7/lib-dynload',
'/app/.heroku/python/lib/python2.7/site-packages',
'/app/.heroku/python/lib/python2.7/site-packages/PIL',
'/app/.heroku/python/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
response = callback(request, *callback_args, **callback_kwargs) ...
â–¶ Local vars
/app/movie_manager/views.py in edit_photo
photo_comp.save(thum, "png") ...
â–¶ Local vars
/app/.heroku/python/lib/python2.7/site-packages/PIL/Image.py in save
save_handler(self, fp, filename) ...
â–¶ Local vars
/app/.heroku/python/lib/python2.7/site-packages/PIL/PngImagePlugin.py in _save
ImageFile._save(im, _idat(fp, chunk), [("zip", (0,0)+im.size, 0, rawmode)]) ...
â–¶ Local vars
/app/.heroku/python/lib/python2.7/site-packages/PIL/ImageFile.py in _save
e = Image._getencoder(im.mode, e, a, im.encoderconfig) ...
â–¶ Local vars
/app/.heroku/python/lib/python2.7/site-packages/PIL/Image.py in _getencoder
return encoder(mode, *args + extra) ...
â–¶ Local vars
For anyone else who finds this problem...
This TypeError can occur when PIL and Pillow are installed simultaneously (perhaps inadvertently due to hidden package requirements). You'll need to uninstall both PIL and Pillow and reinstall just the one you want.

Error using ModelResource: can't set attribute

I can't get my head around this error... I'm using the restframework2 branch.
Am I doing something wrong, or is this a bug in the restframework2 code?
Here's my code:
resources.py
class TemplateHoursSerializer(serializers.ModelSerializer):
class Meta:
model = TemplateHours
nested = True
start = HourField()
end = HourField()
employee = EmployeeSerializer()
class TemplateHoursResource(ModelResource):
model = TemplateHours
serializer_class = TemplateHoursSerializer
urls.py
url(r'^api/template-hours/$', TemplateHoursResource.as_view(actions={
'get': 'list',
'post': 'create'
})),
url(r'^api/template-hours/(?P<pk>[0-9]+)/$', TemplateHoursResource.as_view(actions={
'get': 'retrieve',
'put': 'update',
'delete': 'destroy'
})),
...
When I visit (or POST to) http://127.0.0.1:8000/api/template-hours/, I get this error and traceback:
AttributeError at /api/template-hours/
can't set attribute
Request Method: GET
Request URL: http://127.0.0.1:8000/api/template-hours/
Django Version: 1.4.1
Exception Type: AttributeError
Exception Value:
can't set attribute
Exception Location: C:\Users\Mathieu\Development\django_projects\hedron\Lib\site- packages\rest_framework\resources.py in wrapped, line 13
Python Executable: C:\Python27\python.exe
Python Version: 2.7.2
Python Path:
['C:\\Users\\Mathieu\\Development\\django_projects\\hedron\\Scripts',
'C:\\Python27\\lib\\site-packages\\setuptools-0.6c12dev_r88846-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\virtualenv-1.7.1.2-py2.7.egg',
'C:\\Users\\Mathieu\\Development\\django_projects\\hedron\\Lib\\site-packages',
'C:\\Users\\Mathieu\\Development\\django_projects\\hedron\\hedron',
'C:\\Python27\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages',
path(u'C:\\Users\\Mathieu\\Development\\django_projects\\hedron'),
path(u'C:\\Users\\Mathieu\\Development\\django_projects\\hedron\\hedron\\apps'),
path(u'C:\\Users\\Mathieu\\Development\\django_projects\\hedron\\hedron\\libs')]
Server time: di, 9 Okt 2012 22:46:50 +0200
Traceback:
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\rest_framework\resources.py" in view
48. return self.dispatch(request, *args, **kwargs)
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
77. return view_func(*args, **kwargs)
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\rest_framework\views.py" in dispatch
324. response = self.handle_exception(exc)
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\rest_framework\views.py" in dispatch
321. response = handler(request, *args, **kwargs)
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\rest_framework\resources.py" in list
74. return self.root_view().list(request, args, kwargs)
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\rest_framework\resources.py" in root_view
68. return wrapped(self, self.root_class())
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\rest_framework\resources.py" in wrapped
13. setattr(dest, attr, getattr(source, attr))
Exception Type: AttributeError at /api/template-hours/
Exception Value: can't set attribute
Resources and routers are not finished/supported in [the beta of] REST framework 2 yet. The docs on them are a placeholder for what I want the design to look like, but I'll be removing them from the index today. Hopefully they'll make it in for 2.0, but I don't see it as at all essential, since you can do everything you need with Views and explicit URLconfs. Resources & routers just give you a useful shortcut.

django error: __init__() takes exactly 1 argument (2 given)

I've written a sqlalchemy model called 'library':
class Library(Base):
__tablename__ = 'library'
id = Column(Integer, primary_key=True)
details = Column(String)
def __init__(self, details):
self.details = details
def __repr__(self):
return u"Library(%s)" % (self.details)
Then, inside the views.py file, I've written:
def is_lib_empty():
return len(session.query(Library).all()) <= 0
def populateLib():
new_libs = [Library('one'), Library('two'), Library('three'), Library('four')]
session.add_all(new_libs)
session.commit()
def index(request):
if is_lib_empty():
populateLib()
libs = session.query(Library).all()
return render_to_response('../templates/index.html',{'libs':libs})
And then I run python manage.py runserver, and it shows me an error message:
__init__() takes exactly 1 argument (2 given)
What should I do to fix this?
TypeError at /
__init__() takes exactly 1 argument (2 given)
Request Method: GET
Django Version: 1.3.1
Exception Type: TypeError
Exception Value:
__init__() takes exactly 1 argument (2 given)
Exception Location: /cs/wetlab/Limor/workspace/Yeasti/Yeasti/loose_coupling/../loose_coupling/with_sqlalchemy/views.py in populateLib, line 25
Python Executable: /sw/bin/python2.7
Python Version: 2.7.2
Python Path:
['/cs/wetlab/Limor/workspace/Yeasti/Yeasti/loose_coupling',
'/sw/lib/python27.zip',
'/sw/lib/python2.7',
'/sw/lib/python2.7/plat-darwin',
'/sw/lib/python2.7/plat-mac',
'/sw/lib/python2.7/plat-mac/lib-scriptpackages',
'/sw/lib/python2.7/lib-tk',
'/sw/lib/python2.7/lib-old',
'/sw/lib/python2.7/lib-dynload',
'/sw/lib/python2.7/site-packages',
'/sw/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
Server time: Sun, 1 Jul 2012 05:50:03 -0500
Environment:
Request Method: GET
Django Version: 1.3.1
Python Version: 2.7.2
Installed Applications:
['loose_coupling.with_sqlalchemy']
Installed Middleware:
('django.middleware.common.CommonMiddleware',)
Traceback:
File "/sw/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/cs/wetlab/Limor/workspace/Yeasti/Yeasti/loose_coupling/../loose_coupling/with_sqlalchemy/views.py" in index
39. populateLib()
File "/cs/wetlab/Limor/workspace/Yeasti/Yeasti/loose_coupling/../loose_coupling/with_sqlalchemy/views.py" in populateLib
25. new_libs = [Library('one'), Library('two'), Library('three'), Library('four')]
Exception Type: TypeError at /
Exception Value: __init__() takes exactly 1 argument (2 given)
UPDATE:
You can always use the default constructor, using keyword arguments:
Library(details='some details')
as described in http://docs.sqlalchemy.org/en/rel_0_7/orm/tutorial.html
This is supported by the default constructor, no need to override it. Anyway, your code should work, unless there is some override somewhere...