Reputation: 1767
I put the ImageField into the Model and can successfully upload the image file. But when try to open the file through admin page. Cannot open the file but there is an error says "500 Internal server error".
In the file name there are some non-ascii letters inside. How can I fix this problem?
class Customer(User):
profile_image = models.ImageField(upload_to='customers/photos', null=True, blank=True)
hospital = models.ForeignKey('Hospital', null=True, blank=True)
treatments = models.ManyToManyField('Treatment', blank=True)
verified = models.BooleanField(default=False)
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
image file name = "데이비드_베컴2.jpg"
actually this model has more then one field..
+) admin.py
class CustomerAdmin(UserAdmin):
form = CustomerChangeForm
add_form = CustomerCreationForm
# The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin
# that reference specific fields on auth.User.
list_display = ('email', 'phonenumber', 'is_admin')
list_filter = ('is_admin',)
fieldsets = (
(None, {'fields': ('email', 'password', 'phonenumber', 'smscheck',
'name', 'hospital', 'major', 'treatments', 'info', 'profile_image', 'verified', )}),
('Personal info', {'fields': ()}),
('Permissions', {'fields': ('is_active', 'is_admin',)}),
)
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides get_fieldsets to use this attribute when creating a user.
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'password1', 'password2', 'phonenumber', 'smscheck',
'name', 'hospital', 'major', 'treatments', 'info', 'verified', 'is_active', 'is_admin')}
),
)
search_fields = ('email', 'phonenumber')
ordering = ('email',)
filter_horizontal = ()
also when I put the file encoded with english it has no problem.. for example, "myprofile.jpg"
+) error in detail
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
return self.application(environ, start_response)
File "/usr/local/lib/python2.7/site-packages/whitenoise/base.py", line 57, in __call__
static_file = self.find_file(environ['PATH_INFO'])
File "/usr/local/lib/python2.7/site-packages/whitenoise/django.py", line 72, in find_file
if self.use_finders and url.startswith(self.static_prefix):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xeb in position 22: ordinal not in range(128)
How can I fix this problem? thanks in advance!
Upvotes: 0
Views: 1667
Reputation: 1767
Finally I found the solution for this error.
Just implement the new custom field for yourself.
import unicodedata
from django.db.models import ImageField
class MyImageField(ImageField):
def __init__(self, *args, **kwargs):
super(MyImageField, self).__init__(*args, **kwargs)
def clean(self, *args, **kwargs):
data = super(MyImageField, self).clean(*args, **kwargs)
data.name = unicodedata.normalize('NFKD', data.name).encode('ascii', 'ignore')
return data
For more information about this u can check this out here.
Upvotes: 1
Reputation: 3506
Make your class is unicode compatible.
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class Customer(User):
profile_image = models.ImageField(upload_to='customers/photos', null=True, blank=True)
def __str__(self):
return self.profile_image.path
def __unicode__(self):
return unicode(self.profile_image.path)
also, make sure your media directory is defined in settings.py:
MEDIA_ROOT = './media/'
MEDIA_URL = '/media/'
Make sure your LANG environment variable is set - use your locale
export LANG="en_US.UTF-8"
also add export to ~/.bashrc
Upvotes: 0