我想设置个人资料的头像,但在更新对象时出错: '个人资料'对象没有属性'个人资料'
我创建了一个模型:
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)
模型表格:
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.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>
并且网址收件人:
urls.py
url(r'^profile/(?P<slug>[-\w]+)/avatar_add$', AddAvatar, name='avatar_add'),
处理视图:
@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'])
在保存profie的同时,我得到了如上所述的错误。另一个问题是def save()
函数永远不会被调用,因为我没有保存ModalForm()实例,只是用它来更新其他东西。我是Django的新手,请帮忙。
回溯:
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'
答案 0 :(得分:1)
错误最有可能出现在模型定义中的upload_to函数“get_upload_avatar”中。它应该是instance.user而不是instance.profile.user。