I want to set profile's avatar, but I get an error while updating object: 'Profile' object has no attribute 'profile'
I've created a model:
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name='profile')
slug = models.SlugField(editable=False, unique=True)
avatar = models.ImageField(upload_to=get_upload_avatar, null=True, blank=True)
And Model Form:
froms.py
from PIL import Image
class AvatarForm(ModelForm):
class Meta:
model = Profile
fields = ['avatar',]
def save(self):
avatar = super(AvatarForm, self).save()
im = Image.open(avatar.avatar)
resized_image = im.thumbnail(64, 64)
resized_image.save(avatar.avatar.path)
return avatar
Template rendering as usual:
template.html
<form action="{% url 'avatar_add' me.slug %}" enctype="multipart/form-data" method="POST">
{% csrf_token %}
{% render_field avatar_form.avatar class="form-control form-control-sm" accept="image/*" onchange="this.form.submit()"%}
</form>
And url reciever:
urls.py
url(r'^profile/(?P<slug>[-\w]+)/avatar_add$', AddAvatar, name='avatar_add'),
The handling view:
@login_required
def AddAvatar(request, slug):
if request.method == 'POST' and request.FILES['avatar'] and request.user.profile.slug == slug:
form_avatar = AvatarForm(request.POST, request.FILES)
if form_avatar.is_valid():
profile = Profile.objects.get(slug=slug)
profile.avatar = form_avatar.cleaned_data['avatar']
profile.save() # <--get err here
return HttpResponse(form_avatar.cleaned_data['avatar'])
While saving profie I get error as above. Another problem is that def save() function will be never called, because I'm not saving ModalForm() instance, but just using it to update something other. I'm newbie in Django, please help.
Traceback:
File "C:\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner
41. response = get_response(request)
File "C:\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python36-32\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "C:\Users\BA\DjangoProjects\szmatkaPL\core\views.py" in AddAvatar
209. profile.save()
File "C:\Python36-32\lib\site-packages\django\db\models\base.py" in save
806. force_update=force_update, update_fields=update_fields)
File "C:\Python36-32\lib\site-packages\django\db\models\base.py" in save_base
836. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Python36-32\lib\site-packages\django\db\models\base.py" in _save_table
900. for f in non_pks]
File "C:\Python36-32\lib\site-packages\django\db\models\base.py" in <listcomp>
900. for f in non_pks]
File "C:\Python36-32\lib\site-packages\django\db\models\fields\files.py" in pre_save
296. file.save(file.name, file.file, save=False)
File "C:\Python36-32\lib\site-packages\django\db\models\fields\files.py" in save
93. name = self.field.generate_filename(self.instance, name)
File "C:\Python36-32\lib\site-packages\django\db\models\fields\files.py" in generate_filename
327. filename = self.upload_to(instance, filename)
File "C:\Users\BA\DjangoProjects\szmatkaPL\core\misc.py" in get_upload_avatar
12. return os.path.join('profile', 'images', 'avatar', instance.profile.user.username, filename)
Exception Type: AttributeError at /profile/xruchawicax/avatar_add
Exception Value: 'Profile' object has no attribute 'profile'
get_upload_avatar. Now you need to show that code.