我有头像字段的抽象用户。
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)
和views.py
@method_decorator(login_required, name='dispatch')
class UserUpdateView(UpdateView):
model = User
fields = ('first_name', 'last_name', 'email', 'bio', 'location', 'birth_date', 'avatar', )
template_name = 'my_account.html'
success_url = reverse_lazy('my_account')
def get_object(self):
return self.request.user
我可以看到字段,除了头像外,还可以保存所有字段。 怎么办?
答案 0 :(得分:0)
忘记在模板中添加enctype =“multipart / form-data”。