因此,我只是按照this教程为用户创建了一个配置文件并允许他们对其进行编辑,但是当我去访问编辑页面'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:
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
答案 0 :(得分:1)
User.is_authenticated
是一个布尔值,因此您不应将其作为函数来调用。删除括号,它将按预期工作:
if request.user.is_authenticated and request.user.id == user.id: