这是关于课程中的一个博客项目,它似乎适用于许多其他学生,但不适用于我。所以我想看看我的代码出了什么问题。错误信息是:TypeError at /register/save() got an unexpected keyword argument 'force_insert'与问题文件相关的内容如下:users/ views.py:(在register函数的第四个原始中是失败的save()方法)from django.shortcuts import render, redirectfrom django.contrib import messagesfrom django.contrib.auth.decorators import login_requiredfrom .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateFormdef register(request): if request.method == 'POST': form = UserRegisterForm(request.POST) if form.is_valid(): form.save() username = form.cleaned_data.get('username') messages.success(request, f"Your account has been created! You are now able to log in") return redirect('login') else: form = UserRegisterForm() return render(request, 'blog/register.html', {'form': form})@login_requireddef profile(request): if request.method == 'POST': u_form = UserUpdateForm(request.POST, instance=request.user) p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile) if u_form.is_valid() and p_form.is_valid(): u_form.save() p_form.save() messages.success(request, f"Your account has been updated!") return redirect('profile') else: u_form = UserUpdateForm(instance=request.user) p_form = ProfileUpdateForm(instance=request.user.profile) context = { 'u_form': u_form, 'p_form': p_form } return render(request, 'blog/profile.html', context)用户/模型.py:from django.db import modelsfrom django.contrib.auth.models import Userfrom PIL import Imageclass Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) image = models.ImageField(default='default.jpg', upload_to='profile_pics') def __str__(self): return f"{self.user.username} Profile"
1 回答
喵喵时光机
TA贡献1846条经验 获得超7个赞
当重写saveDjango 中的方法时,也应该保留签名。
更改为def save(self, *args, **kwargs):
def save(self, *args, **kwargs):
super(Profile, self).save(*args, **kwargs)
img = Image.open(self.image.path)
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.image.path)
添加回答
举报
0/150
提交
取消