简体   繁体   English

一个意外的关键字参数python

[英]An unexpected keyword argument python

I'm trying to implement a simple function to like a post. 我正在尝试实现一个简单的函数来喜欢帖子。 I have 4 models defined using Google App Engine; 我有4个使用Google App Engine定义的模型; User, Blogpost, Like, Comments 用户,Blogpost,喜欢,评论

below is the snippets: 以下是片段:

class LikePost(db.Model):
    user        = db.ReferenceProperty(User)
    blogpost    = db.ReferenceProperty(Blogpost)
    date        = db.DateTimeProperty(auto_now_add = True)

class Comment(db.Model):
    user        = db.ReferenceProperty(User)
    blogpost    = db.ReferenceProperty(Blogpost)
    content     = db.TextProperty(required = True)
    date        = db.DateTimeProperty(auto_now_add = True)

I tried to call the method to like a post using below: 我尝试使用下面的方法调用该方法:

class LikePost(Handler):
    def get(self,post_id):
        blogpost = self.get_blogpost(post_id)
        user = self.get_user_object()
        if blogpost and user:
            like = LikePost(user = user, blogpost = blogpost)
            like.put()
            self.redirect('/%s' % post_id)
        else:
            self.redirect('/login')

The reference to the method is as follow: 该方法的参考如下:

def get_user_object(self):
        cookie = self.request.cookies.get('user_id')
        if cookie:
            user_id = check_secure_val(cookie)

            if user_id:
                user_id = cookie.split('|')[0]
                key = db.Key.from_path('User', int(user_id))
                user = db.get(key)
                return user

def get_blogpost(self, post_id): key = db.Key.from_path('Blogpost', int(post_id)) blogpost = db.get(key) return blogpost def get_blogpost(self,post_id):key = db.Key.from_path('Blogpost',int(post_id))blogpost = db.get(key)return blogpost

I got an error when trying to run the above : 尝试运行上述操作时出错:

__init__() got an unexpected keyword argument 'blogpost'

Anyone can explain what went wrong ? 谁能解释出了什么问题?

You have defined your model as 您已将模型定义为

class LikePost(db.Model):

Then you have defined your handler has 然后你已经定义了你的处理程序了

class LikePost(Handler):

Notice that they have the same name. 请注意,它们具有相同的名称。 So inside your get method what's in scope is your Handler subclass, which apparently does not expect a blogpost keyword argument to it's __init__ method. 所以在你的get方法中,你的Handler子类是什么,显然不希望它的__init__方法有一个blogpost关键字参数。 Simplest solution, rename one or the other or 最简单的解决方案,重命名其中一个或

 from models import LikePost as LP

and use that 并使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM