SimpleLazyObject'对象不可调用

时间:2015-02-23 17:19:17

标签: python django forms authentication

团队,我试图为新帖子创建一个表单,其中包含以下字段。标题,正文,类别。我需要使用在登录时使用的用户名自动保存作者姓名(此处称为所有者),但是虽然我能够使用日期和时间执行此操作,但我没有使用用户,您能帮忙吗?请尽可能详细说明

查看:

def create_post(request):
    if request.method == 'POST':
        form = CreatePostForm(request.POST)

        if form.is_valid():
            post = form.save(commit=False)
            post.datposted = datetime.datetime.now()
            post.owner = request.user()
            post.save()
            return HttpResponseRedirect('/posts/')
        else:
            return HttpResponse("Favor. Verifique os campos necessarios")

    else:
        form = CreatePostForm()
        f = {'form' : form}
        return render(request,'create_post.html',f)

型号:

class Post(models.Model):
    title = models.CharField(max_length=100)
    body = models.TextField()
    datposted = models.DateTimeField('date posted')
    category = models.ForeignKey('Category')
    owner = models.ForeignKey('UserProfile')

    def __str__(self):
        return '%s' % self.title

class Category(models.Model):
    title = models.CharField(max_length=100)
    def __str__(self):
        return self.title

class UserProfile(models.Model):

    user = models.OneToOneField(User)


    website = models.URLField(blank=True)
    picture = models.ImageField(upload_to='profile_images', null=True)


    def __unicode__(self):
        return self.user.username

class Logout(User):
    force_logout_date = models.DateTimeField(null=True, blank=True)

1 个答案:

答案 0 :(得分:1)

你不应该调用用户对象。

post.owner = request.user