在 Django 中创建用户通知系统
我正在添加一个系统,为用户留下“通知”,以便在下次登录时显示。我在 models.py 文件中创建了一个简单的通知类。我有这个 UserInfo 类(在同一个 models.py 中)来将一些属性添加到 Django 的现有用户系统中,作为 Socialauth 的一部分:
class UserInfo(models.Model):
user = models.OneToOneField(User, unique=True)
...
reputation = models.IntegerField(null=True, blank=True)
def add_notification(message):
notification = Notification(user=self.user, message=message)
notification.save
当我在控制台中尝试时,我最终会得到这样的结果:
>>> user = User.objects.get(id=14)
>>> user.userinfo.add_notification('you are an awesome intern!')
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: add_notification() takes exactly 1 argument (2 given)
>>>
我在这里缺少什么?我是一个 Django 菜鸟,所以也许这很简单。谢谢!
I am adding a system to leave "notifications" for users that can be displayed the next time they log in. I created a simple Notification class in the models.py file. I have this UserInfo class (in the same models.py) to add on some attributes to Django's existing user system as part of socialauth:
class UserInfo(models.Model):
user = models.OneToOneField(User, unique=True)
...
reputation = models.IntegerField(null=True, blank=True)
def add_notification(message):
notification = Notification(user=self.user, message=message)
notification.save
When I try it out in the console I end up with this:
>>> user = User.objects.get(id=14)
>>> user.userinfo.add_notification('you are an awesome intern!')
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: add_notification() takes exactly 1 argument (2 given)
>>>
What am I missing here? I'm kind of a Django noob so maybe it's something easy. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Django 消息
首先,请考虑 dcrodjer 的回答。 Django 消息系统正是您所需要的,为什么要将免费获得的东西放入您的代码树中呢?
(当然,如果您这样做只是为了实验并了解有关 Django 的更多信息,请继续!)
无论如何,修复
摘要:要修复此问题,只需将
add_notifications
更改为:注意附加参数(名为
self
)在方法签名中。为什么它不起作用
在 Python 中调用方法有一点奇怪。
当您调用方法
bar
时,您可以使用像thisguy.bar()
这样的行。 Python 发现您正在调用一个对象的方法(在名为thisguy
的对象上调用名为bar
的方法)。发生这种情况时,Python 会使用对象本身(thisguy
对象)填充方法的第一个参数。您的方法不起作用的原因是您在只需要一个参数的方法上调用
userinfo.add_notification('you are an Awesome intern!')
。嗯,Python 已经用userinfo
对象填充了第一个参数(名为message
)。因此,Python 会抱怨您将两个参数传递给一个只需要一个参数的方法。Use Django messages
First off, please consider dcrodjer's answer. The Django message system is exactly what you need, and why put in your code tree something that you get for free?
(Of course, if you're doing this just to experiment and learn more about Django, please continue!)
Anyway, a fix
Summary: To fix this, just change
add_notifications
to this:Note the additional argument (named
self
) in the method signature.Why it's not working
There's a bit of a quirk in calling methods in Python.
When you call the method
bar
, you can use a line likethisguy.bar()
. Python sees that you're calling a method on an object (a method calledbar
on an object calledthisguy
). When this happens, Python fills in the first argument of the method with the object itself (thethisguy
object).The reason your method doesn't work is that you are calling
userinfo.add_notification('you are an awesome intern!')
on a method that is only expecting one argument. Well, Python has already filled in the first argument (namedmessage
) with theuserinfo
object. Thus, Python complains that you're passing two arguments to a method that only expects one.使用 django 消息框架: http://docs.djangoproject.com/en/dev/ref/contrib /消息/
您可以在用户登录后立即将用户信息存储的消息放入队列中:
Use the django message framework: http://docs.djangoproject.com/en/dev/ref/contrib/messages/
You may put the userinfo stored messages in queue as soon as he logs in using this:
add_notification 是类上的一个方法。这意味着它隐式地将类的实例作为第一个参数传递。 Python 中的类
试试这个:
add_notification is a method on a class. That means it implicitly gets passed the instance of the class as the first parameter. Classes in Python
Try this instead:
如果您正在寻找持久消息传递,您也许应该更新您的问题。也许 https://github.com/philomat/django-persistent-messages 可以帮助你节省编码时间?
You should perhaps update your question if you're looking for persistent messaging. Maybe https://github.com/philomat/django-persistent-messages can help you save coding time?