收到此错误,
'查询集'对象没有属性' _meta'
如何解决这个问题
views.py
def profile_update_view(request, username):
user = User.objects.filter(username__iexact=username)
form = UserProfileForm(instance=user)
if request.user.is_authenticated and request.user.id == user.id:
if request.method == "POST":
form = UserProfileForm(request.POST, request.FILES, instance=user)
if form.is_valid():
created_prof = form.save(commit=False)
created_prof.user = request.user
created_prof.save()
return redirect('profiles:profile-detail',username=username)
return render(request, "profiles/profile_form.html", {
"username": username,
"form": form,
})
答案 0 :(得分:0)
filter
始终返回一个查询集。您应该使用get
来获取单个实例。
user = User.objects.get(username__iexact=username)