慕蓋茨4494581
2023-08-03 17:10:03
這是關(guān)于課程中的一個(gè)博客項(xiàng)目,它似乎適用于許多其他學(xué)生,但不適用于我。所以我想看看我的代碼出了什么問(wèn)題。錯(cuò)誤信息是:TypeError at /register/save() got an unexpected keyword argument 'force_insert'與問(wèn)題文件相關(guān)的內(nèi)容如下:users/ views.py:(在register函數(shù)的第四個(gè)原始中是失敗的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)用戶(hù)/模型.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 回答

喵喵時(shí)光機(jī)
TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
當(dāng)重寫(xiě)saveDjango 中的方法時(shí),也應(yīng)該保留簽名。
更改為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)
添加回答
舉報(bào)
0/150
提交
取消