在 Django 中创建用户通知系统

发布于 2024-10-03 18:58:22 字数 837 浏览 0 评论 0原文

我正在添加一个系统,为用户留下“通知”,以便在下次登录时显示。我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

太傻旳人生 2024-10-10 18:58:22

使用 Django 消息

首先,请考虑 dcrodjer 的回答。 Django 消息系统正是您所需要的,为什么要将免费获得的东西放入您的代码树中呢?

(当然,如果您这样做只是为了实验并了解有关 Django 的更多信息,请继续!)


无论如何,修复

摘要:要修复此问题,只需将 add_notifications 更改为:

    def add_notification(self, message):
        notification = Notification(user=self.user, message=message)
        notification.save

注意附加参数(名为self)在方法签名中。


为什么它不起作用

在 Python 中调用方法有一点奇怪。

class Foo(object):
    def bar(self):
        print 'Calling bar'

    def baz(self, shrubbery):
        print 'Calling baz'

thisguy = Foo()

当您调用方法 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:

    def add_notification(self, message):
        notification = Notification(user=self.user, message=message)
        notification.save

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.

class Foo(object):
    def bar(self):
        print 'Calling bar'

    def baz(self, shrubbery):
        print 'Calling baz'

thisguy = Foo()

When you call the method bar, you can use a line like thisguy.bar(). Python sees that you're calling a method on an object (a method called bar on an object called thisguy). When this happens, Python fills in the first argument of the method with the object itself (the thisguy 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 (named message) with the userinfo object. Thus, Python complains that you're passing two arguments to a method that only expects one.

我爱人 2024-10-10 18:58:22

使用 django 消息框架: http://docs.djangoproject.com/en/dev/ref/contrib /消息/
您可以在用户登录后立即将用户信息存储的消息放入队列中:

messages.add_message(request, messages.INFO, 'Hello world.')

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:

messages.add_message(request, messages.INFO, 'Hello world.')
舂唻埖巳落 2024-10-10 18:58:22

add_notification 是类上的一个方法。这意味着它隐式地将类的实例作为第一个参数传递。 Python 中的类

试试这个:

class UserInfo(models.Model):
    ...
    def add_notification(self, message):
        ...

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:

class UserInfo(models.Model):
    ...
    def add_notification(self, message):
        ...
我要还你自由 2024-10-10 18:58:22

如果您正在寻找持久消息传递,您也许应该更新您的问题。也许 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文