Django编辑配置文件-'bool'对象不可调用 [英] Django Edit Profile - 'bool' object is not callable
本文介绍了Django编辑配置文件-'bool'对象不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我只关注了此教程,为用户创建配置文件并允许他们对其进行编辑,但是当我去访问编辑页面时,出现此错误'bool'对象不可调用
So I just followed this tutorial to create a profile for a user and allow them to edit it but I get this error when I go and visit the edit page 'bool' object is not callable
File "C:\Users\...\account\userprofile\views.py", line 64, in profile_edit_view
if request.user.is_authenticated() and request.user.id == user.id:
TypeError: 'bool' object is not callable
models.py:
models.py:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name="user", on_delete=models.CASCADE)
photo = models.ImageField(upload_to = "account/userprofile/photos", verbose_name="Profile Picture", max_length=255, null=True, blank=True)
location = models.CharField(max_length=100, default='', blank=True)
website = models.URLField(default='', blank=True)
bio = models.TextField(default='', blank=True)
@receiver(post_save, sender=User)
def create_profile(sender, **kwargs):
user = kwargs["instance"]
if kwargs["created"]:
user_profile = UserProfile(user=user)
user_profile.save()
post_save.connect(create_profile, sender=User)
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserChangeForm
class ProfileEditForm(forms.ModelForm):
class Meta:
model = User
fields = ['first_name', 'last_name', 'email']
views.py
def profile_edit_view(request, pk):
# querying the User object with pk from url
user = User.objects.get(pk=pk)
# prepopulate UserProfileForm with retrieved user values from above.
user_form = ProfileEditForm(instance=user)
# The sorcery begins from here, see explanation below
ProfileInlineFormset = inlineformset_factory(User, UserProfile, fields=('location', 'website', 'bio'))
formset = ProfileInlineFormset(instance=user)
if request.user.is_authenticated() and request.user.id == user.id:
if request.method == "POST":
user_form = ProfileEditForm(request.POST, request.FILES, instance=user)
formset = ProfileInlineFormset(request.POST, request.FILES, instance=user)
if user_form.is_valid():
created_user = user_form.save(commit=False)
formset = ProfileInlineFormset(request.POST, request.FILES, instance=created_user)
if formset.is_valid():
created_user.save()
formset.save()
return HttpResponseRedirect('/profile/')
return render(request, "account/account_update.html", {
"noodle": pk,
"noodle_form": user_form,
"formset": formset,
})
else:
raise PermissionDenied
推荐答案
User.is_authenticated
is a Boolean value, so you should not call it as a function. Remove the parentheses and it will work as expected:
if request.user.is_authenticated and request.user.id == user.id:
这篇关于Django编辑配置文件-'bool'对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文